Search code examples
c++templatespartial-specialization

partial function template specialization


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;
}

Solution

  • 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).