specialize a member template without specializing its parent

c++, partial-specialization, templates

Solution

It is illegal under C++ standard 14.7.3/18:

.... the declaration shall not explicitly specialize a class member template if its enclosing class templates are not explicitly specialized as well.

Problem

I have a class template nested inside another template. Partially specializing it is easy: I just declare another `template< … >` block inside its parent. However, I need another partial specialization that happens to specify all its local template arguments. This makes it into an explicit specialization. Explicit specializations, for whatever reason, must be at namespace scope. To declare it outside its parent class, the parent must be nominated, which requires a non-empty template argument list. This implies partial specialization. Partial specialization is what I'm doing, and it's supposed to work at arbitrary outer scope. But both GCC and Comeau fail to identify the template parameter in the parent nomination with the partial specialization formal arguments. ``` template< class X > struct A { template< class Y > struct B; // initial declaration OK template< class Z > struct B< A< Z > > {}; // partial OK as long as there's a local arg template<> // ERROR: this syntax triggers explicit specialization struct B< int > {}; }; template<> // ERROR: can't nest template<>s here (why?) template< class X > // ERROR: can't deduce X from type of A<X>::B<int> (why?) struct A< X >::B< int > {}; ``` (I left all my non-working code in; comment it appropriately to attempt to make sense.)

Original source