【问题标题】:Compilation failure for templated member of templated class. VC++ works but G++ fails模板化类的模板化成员编译失败。 VC++ 工作,但 G++ 失败
【发布时间】:2012-09-27 20:00:24
【问题描述】:

以下代码在 Visual C++ 2010 下编译良好,但在 Android NDK r8b 的 GCC 4.6 下编译不正常。

template<typename A>
struct foo
{
    template<typename B>
    B method()
    {
        return B();
    }
};

template<typename A>
struct bar
{
    bar()
    {
        f_.method<int>(); // error here
    }

private:
    foo<A> f_;
};

GCC 给出的错误是

error : expected primary-expression before 'int'
error : expected ';' before 'int'

为标记线。对于我的生活,我无法弄清楚什么是错的。

【问题讨论】:

标签: c++


【解决方案1】:

GCC 是正确的,因为 f_ 是 foo&lt;A&gt; 类型,它取决于模板参数 A,您需要使用 template 关键字限定对 method 的调用:

f_.template method<int>();  // This will work

【讨论】:

  • 啊,当然。谢谢,我总是混淆何时使用该限定词。
猜你喜欢
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多