【问题标题】:gcc 4.5.1 vs VC2010 template partial specialization: which is C++0x conformant?gcc 4.5.1 与 VC2010 模板部分专业化:哪个符合 C++0x?
【发布时间】:2011-02-20 08:08:17
【问题描述】:

以下内容使用 VC2010 编译,但使用 gcc 4.5.1 失败,-std=c++0x:

template <class T, class TBase>
class TestBase : public TBase
{
public:
// Helper functor
    template <unsigned int t_u, class TRet = int>
    struct Helper
    {
        TRet operator() (int x = 0, int y = 0)
        {
                return (TRet)t_u;
        }
    };
}; // class TestBase

template<class TBase>
class Test0 : public TestBase<Test0<TBase>, TBase>
{
public:
    Helper<100> Get100; // gcc 4.5.1 error: 'Helper' does not name a type
};

gcc 接受更详细的:

template<class TBase>
class Test1 : public TestBase<Test1<TBase>, TBase>
{
    typedef TestBase<Test1<TBase>, TBase> thisBase;
public:
    typename thisBase::template Helper<100> Get100;
};

哪个(如果有)符合 c++0x 标准?

【问题讨论】:

  • 无论何时涉及模板,很遗憾 VC++ 不符合标准。这很烦人,但你很快就会掌握它。

标签: c++ visual-studio-2010 templates gcc c++11


【解决方案1】:

在第一个代码sn-p中,Helper不是依赖名。因为它不是依赖的,所以它必须在模板解析的时候解决,而不是实例化的时候。这在 C++03 和 C++0x 中都是必需的。

使用 typedef 使其成为依赖名称,因此它在实例化时被解析。你也可以使用 typename Test1::template Helper.

【讨论】:

  • 谢谢。由于我有大约 20 个 TestX 类,每个类都有 10 到 60 个 Helper 成员,因此我寻找简单的声明。猜猜需要一些宏...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多