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?
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; ~~~~~^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With