如何修复 C++ 模板代码中的“预期主表达式之前”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2105901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fix 'expected primary-expression before' error in C++ template code?
提问by jwfearn
Here's yet another VC9 vs. GCC 4.2 compile error problem. The following code compiles fine with VC9 (Microsoft Visual C++ 2008 SP1) but not with GCC 4.2 on Mac:
这是另一个 VC9 与 GCC 4.2 编译错误问题。下面的代码在 VC9 (Microsoft Visual C++ 2008 SP1) 上编译得很好,但在 Mac 上用 GCC 4.2 编译不行:
struct C
{
template< typename T >
static bool big() { return sizeof( T ) > 8; }
};
template< typename X >
struct UseBig
{
static bool test()
{
return X::big< char >(); // ERROR: expected primary-expression
} // before 'char'
};
int main()
{
C::big< char >();
UseBig< C >::test();
return 0;
}
Any ideas how I can fix this?
有什么想法可以解决这个问题吗?
回答by Georg Fritzsche
That should be
那应该是
return X::template big< char >();
Dependent names from templates are taken to notbe typesunless you specify that they are via typename
and assumed to notbe templatesunless specified via template
.
从模板依赖的名字被带到不成为的类型,除非你指定,他们是通过typename
与假定未成为模板,除非通过指定的template
。