Alex Rivera | Logout

Template partial specialization

Asked 2011-03-06T15:00:15.850
9

Would any one knows according to what rules code below doesn't compile?

template <class T>
struct B
{
    typedef T type;
};

template<class T>
struct X
{
};
template<class T>
struct X<B<T>::type*>//HERE I'M PARTIALLY SPECIALIZING (WELL, TRYING TO...)
{
};

Please see comment inside the code.

Edit
Report

1 Answer

6

You need to use typename keyword as,

template<class T>
struct X<typename B<T>::type*>
{
};

It's because B<T>::type is a dependent name. So typename is required!

--

EDIT:

Even after putting typename, it isn't compiling. I think it's because deduction of type T in B<T> from X<U> is difficult, or possibly impossible, for the compiler. So I believe its non-deduced context.

See a similar example here and the discussion:

Template parameters in non-deduced contexts in partial specializations


However, if you change the specialization to this:

template<class T>
struct X<B<T> >
{
};

Then it becomes the deducible context, and so would compile.

answered 2011-03-06T15:09:48.433

Your Answer