【发布时间】:2016-04-21 21:18:14
【问题描述】:
我正在测试一个试图用于模板条件的结构,但我遇到了一些奇怪的编译器错误。这是我的代码:
#include <type_traits>
#include <string>
template<typename T1, typename T2,
bool SAME_SIZE = (sizeof(T1)==sizeof(T2))>
struct same_size
{
typedef typename std::false_type value;
};
template<typename T1, typename T2>
struct same_size<T1, T2, true>
{
typedef typename std::true_type value;
};
int main()
{
if(same_size<char,unsigned char>::value)
{
printf("yes");
}
system("PAUSE");
}
我在 Visual Studio 2015 中编译它。这些是我得到的编译器错误:
1> main.cpp
1>c:\users\luis\documents\visual studio 2015\projects\stringtype\stringtype\main.cpp(18): error C2059: syntax error: ')'
1>c:\users\luis\documents\visual studio 2015\projects\stringtype\stringtype\main.cpp(19): error C2143: syntax error: missing ';' before '{'
谁能解释一下这里发生了什么?
【问题讨论】:
-
typedefs 中的那些typenames 不需要。解决错误的另一种方法是实例化value-if(same_size<char,unsigned char>::value{}),但 Sergey 的回答是解决此问题的正确方法。
标签: c++ templates sfinae typetraits