Ryan Calhoun
Ryan Calhoun

Reputation: 2363

Template specialization of templated type not selected when using multiple typenames

Let's say I have

template <typename T>
class F;

I can create another template who takes an F as a type, with a default and a specialization.

template <typename S>
class G;

template <>
template <typename T>
class G <F<T> >
{
};

I can instantiate G<F<int> > g_of_f. The compiler correctly selects the specialization of G, and everything is great.

So here's my problem. I want to do this with more than one template typename in the list. But when I try

template <typename U, typename S>
class H;

template <typename U>
template <typename T>
class H <U, F<T> >
{
};

now I cannot instantiate H<void, F<int> > h_of_f, because the compiler is selecting the original template H and not the specialization.

I've observed the same behavior with g++ 4.1 and g++ 4.4.

What is the difference between G and H that's preventing the compiler from working the way I'm expecting?

Upvotes: 0

Views: 177

Answers (2)

Anycorn
Anycorn

Reputation: 51565

template <typename U, typename S>
class H;

template <typename U, typename T>
class H <U, F<T> > {};

This syntax:

template <> template <typename U>

is for specializing/defining template members outside of template: http://www.comeaucomputing.com/techtalk/templates/#outsidedef

Upvotes: 1

icecrime
icecrime

Reputation: 76845

The template<> syntax is used to introduce an explicit specialization declaration, what you have here are partial specializations :

template <typename S>
class G;

template <typename S>
class G < F<S> >
{
};

template <typename U, typename S>
class H;

template <typename U, typename S>
class H <U, F<S> >
{
};

Upvotes: 3

Related Questions