如何解决C ++模板代码中的“期望的主表达式”错误? [英] How to fix 'expected primary-expression before' error in C++ template code?

查看:110
本文介绍了如何解决C ++模板代码中的“期望的主表达式”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是另一个VC9与GCC 4.2编译错误问题。以下代码使用VC9(Microsoft Visual C ++ 2008 SP1)进行编译,但在Mac上不使用GCC 4.2:

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:

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?

推荐答案

应该是

return X::template big< char >();

模板的依赖名称不是 / em>,除非您通过 typename 指定并假设不是模板 $ c> template 。

Dependent names from templates are taken to not be types unless you specify that they are via typename and assumed to not be templates unless specified via template.

这篇关于如何解决C ++模板代码中的“期望的主表达式”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆