Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

specialize a member template without specializing its parent

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.)

like image 851
Potatoswatter Avatar asked Jan 05 '10 23:01

Potatoswatter


1 Answers

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.

like image 77
user213546 Avatar answered Oct 21 '22 04:10

user213546