c++ - 不同命名空间中的模板特化静态成员

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

命名空间中有一个模板类

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:

An explicit specialization shall be declared in a namespace enclosing the specialized template. An explicit specialization whose declarator-id is not qualified shall be declared in the nearest enclosing namespace of the template, or, if the namespace is inline (7.3.1), any namespace from its enclosing namespace set.

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

最佳答案

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

error: cannot define or redeclare 'bar' here because namespace 'O' does not enclose namespace 'Foo'

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

关于c++ - 不同命名空间中的模板特化静态成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400814/

相关文章:

c++ - 调整具有非类型参数作为模板模板参数的模板

c++ - 如何制作一个 const 引用的元组?

c++ - 类构造函数优先级与值包装器的可变模板构造函数

c++ - 我可以检索使用 Conan 包管理器打包的库的源代码以便能够在其中进行调试吗?

c++ - 来自 .dll 的函数在 vb.net 中失败,但在 vb6 中有效

c++ - 将 optional 与 reference_wrapper 结合起来有意义吗?

c++ - 使用定义相互依赖

c++ - SFINAE 关于模板成员重载

c++ - 推导函数指针返回类型

c++ - 字符串 C++ 操作