0

Hi I think I'm missing something with this technique. Tried to follow examples but the following gives me an error: invalid use of incomplete type ‘class Citizen<T, minAge, maxAge, true>’

template <typename T, T minAge, T maxAge, bool isarmed>
class Citizen {
public:

    Citizen(T health, T age);
    Citizen(T health, T age, T attackPower);
    T getAttackPower();

private:
    T _health;
    T _age;
    T _attackPower;
};

template <typename T, T minAge, T maxAge>
T Citizen<T, minAge, maxAge, true>::getAttackPower() {
    return _attackPower;
}

1 Answers1

1

You cannot use partial template specialization for a single member function - need to specialize the entire class instead (however, full template specialization would be okay).

Eugene
  • 6,194
  • 1
  • 20
  • 31
  • Then how to work it out, without changing class interface? –  Nov 30 '16 at 19:44
  • how can I do this using enable_if_t expression ? –  Nov 30 '16 at 19:52
  • @tomtom What is your implementation in case `isarmed==false`? You could probably combine both and not use the partial specialization at all. – Eugene Nov 30 '16 at 19:54
  • I dont want compiltor to compile this function in case isarmed == false –  Nov 30 '16 at 19:58
  • Then use your implementation in non-specialized template, and insert `static_assert(isarmed,"Citizen must be armed!");` in it. – Eugene Nov 30 '16 at 20:01
  • if i insert this line: template> it works but only if this function is defined is in class definition –  Nov 30 '16 at 20:07
  • Using `enable_if` or other SFINAE only makes sense if you have other overloads of the function in the same class. Otherwise, `static_assert` is both easier to read and gives you a better error message. – Eugene Nov 30 '16 at 20:11
  • Ok. Indeed I need 2 different ctors depended on weather isarmed == true or isarmed == false –  Nov 30 '16 at 20:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129471/discussion-between-tomtom-and-eugene). –  Nov 30 '16 at 20:16