Specialization of member class nested in a non-specialized class

318 Views Asked by At
template <typename T>
struct A
   {
   template <typename U>
   struct B;

   template <>
   struct B<int> {static const int tag = 1;};  // Works fine in VS2010
   };

How can I specialize B the same way, but outside of A. I tried this with no success :

template <typename T> template <>
struct A<T>::B<int> {static const int tag = 1;};

I get:

error C3212: 'A<T>::B<int>' : an explicit specialization of a template member must be a member of an explicit specialization

It does not make sense since I can do exactly that by defining it inside the class

VS2010 problem? Wrong syntax?

Thanks


PS: This one (which should be wrong anyway, crashes VS2010):

template <> template <typename T>
struct A<T>::B<int> {static const int tag = 1;};
2

There are 2 best solutions below

3
On BEST ANSWER

To quote the C++ spec, §14.17.3.18:

In an explicit specialization declaration for a member of a class template or a member template that appears in namespace scope, the member template and some of its enclosing class templates may remain unspecialzed, except that the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well. [...]

(my emphasis)

This suggests that you can't specialize a template class nested inside another template class unless the outer template class is specialized as well. So it looks like VS2010 has this behavior wrong and g++ has it right.

0
On

It just doesn't work that way.:-(

You cannot specialize a function inside the class declaration, even though msvc accepts this with its default settings.

You also cannot specialize a member function without also specializing the enclosing class. Most compilers avred on this (as does the language standard).