【问题标题】:Template specialization static member in different namespace不同命名空间中的模板特化静态成员
【发布时间】:2015-08-04 17:15:59
【问题描述】:

命名空间中有一个模板类

namespace N
{
    template <typename T>
    class Foo {
        static const T bar;
    };
}

以及不同命名空间中的特化:

namespace O
{
    typedef N::Foo<int> Baz;

    template<>
    const int Baz::bar = 1;
}

此代码使用 gcc (4.9.2) 编译但无法使用 msvc (v120) 编译:

error C2888: 'const int N::Foo<int>::bar' : symbol cannot be defined within namespace 'O'

如果我理解正确,代码不符合 C++11:

应在包含 专门的模板。一个显式特化,其声明符 id 不合格应在最近的封闭命名空间中声明 模板的名称,或者,如果命名空间是内联的 (7.3.1),则任何命名空间 来自其封闭的命名空间集。

这是编译器错误还是我误解了?

【问题讨论】:

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


    【解决方案1】:

    这是一个编译器错误,still present in HEAD。请报告。 Clang 提供了更清晰的诊断:

    错误:无法在此处定义或重新声明“bar”,因为命名空间“O” 不包含命名空间“Foo”

    const int Baz::bar = 1;
              ~~~~~^
    

    【讨论】:

    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多