4
我想写一个template class
检查与SFINAE性状。专业班与SFINAE
如类不能 “超载” 我在那个帖子写着:template overloading and SFINAE working only with functions but not classes
我写了下面的代码:
class AA { public: using TRAIT = int; };
class BB { public: using TRAIT = float; };
template < typename T, typename UNUSED = void> class X;
template < typename T>
class X<T, typename std::enable_if< std::is_same< int, typename T::TRAIT>::value, int >::type>
{
public:
X() { std::cout << "First" << std::endl; }
};
template < typename T>
class X<T, typename std::enable_if< !std::is_same< int, typename T::TRAIT>::value, unsigned int >::type>
{
public:
X() { std::cout << "Second" << std::endl; }
};
int main()
{
X<AA> a;
X<BB> b;
}
但它只是失败:
error: aggregate 'X<AA> a' has incomplete type and cannot be defined
X<AA> a;
^
error: aggregate 'X<BB> b' has incomplete type and cannot be defined
X<BB> b;
它认为没有一个模板可以工作,但我没有从编译器得到任何暗示,说明为什么两个专业化都失败了。