Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template specialization static member in different namespace

There is a template class in a namespace

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

And a specialization in a different namespace:

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

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

This code compiles with gcc (4.9.2) but fails to compile with msvc (v120):

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

If I understand this correctly, the code is not C++11 compliant:

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.

Is this a compiler bug or do I misunderstand?

like image 765
simon Avatar asked Feb 10 '23 01:02

simon


1 Answers

This is a compiler bug, and still present in HEAD. Please report it. Clang provides a clearer diagnostic:

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

const int Baz::bar = 1;
          ~~~~~^
like image 151
Columbo Avatar answered Feb 12 '23 15:02

Columbo