這是我的代碼。
#include <iostream>
template<class> struct IsInteger;
template<class> struct IsInteger { using value = std::false_type; };
template<> struct IsInteger<int> { using value = std::true_type; };
int main()
{
std::cout << std::boolalpha <<
IsInteger<5>::value::value << '\n';
}
上面的代碼導致錯誤說
Source.cpp(9,36): error C2974: 'IsInteger': invalid template argument for '<unnamed-symbol>', type expected
Source.cpp(9,50): error C2955: 'IsInteger': use of class template requires template argument list
我不明白為什么編譯器
template<> struct IsInteger<int> { using value = std::true_type; };
在這種情況下不選擇。為什么會導致錯誤?
uj5u.com熱心網友回復:
你需要使用你的特質 asIsInteger<int>而不是IsInteger<5>.
std::true_type此外,在這種情況下,慣用的使用方式std::false_type是從它們繼承,而不是將它們別名為值:
template<class> struct IsInteger : std::false_type {};
template<> struct IsInteger<int> : std::true_type {};
int main()
{
std::cout << std::boolalpha << IsInteger<int>::value << '\n';
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/452862.html
