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;
它認爲沒有一個模板可以工作,但我沒有從編譯器得到任何暗示,說明爲什麼兩個專業化都失敗了。