【发布时间】:2016-11-30 18:22:17
【问题描述】:
你能告诉我为什么这段代码不能编译吗?
template <typename T, T minAge, T maxAge, bool isarmed,
typename = std::enable_if_t<std::is_arithmetic<T>::value>>
class Citizen {
public:
Citizen(T health, T age);
Citizen(T health, T age, T attackPower);
T getHealth() const { return _health; };
T getAge() const { return _age; };
T getAttackPower();
void takeDamage(T damage);
private:
T _health;
T _age;
T _attackPower;
};
template <typename T, T minAge, T maxAge, bool isarmed>
Citizen<T, minAge, maxAge, isarmed>::Citizen(T health, T age):
_health (health),
_age (age)
{
static_assert(minAge <= maxAge, "Wrong age");
assert(minAge <= this->_age && this->_age <= maxAge);
}
我错过了什么?
error: invalid use of incomplete type ‘class Citizen<T, minAge, maxAge, isarmed>’
【问题讨论】:
-
如果我没记错的话,SFINAE 是用于函数模板的重载解析。我认为你不能将它用于类模板。
-
@RSahu 当然可以。那是how
void_tworks -
@Barry,感谢您的链接。我还没有弄清关于 SFINAE 的基本知识。