Possible Duplicate:
“invalid use of incomplete type” error with partial template specialization
Why is it that I can do this:
template <typename T>
struct A
{
void foo(int);
};
template <>
void A<int>::foo(int)
{
}
but not this:
template <typename> struct C {};
template <typename T>
struct A
{
void foo(int);
};
template <typename T>
void A<C<T> >::foo(int)
{
}
For the second case, GCC gives the following error:
test.cpp:10:23: error: invalid use of incomplete type 'struct A<C<T> >'
test.cpp:4:8: error: declaration of 'struct A<C<T> >'
EDIT:
When explaining why the second example is not allowed, please also consider that making the member function also a template has no effect on which example works and which does not. That is, this still works:
template <typename T>
struct A
{
template <typename U>
void foo(U);
};
template <>
template <typename U>
void A<int>::foo(U)
{
}
but this does not:
template <typename> struct C {};
template <typename T>
struct A
{
template <typename U>
void foo(U);
};
template <typename T>
template <typename U>
void A<C<T> >::foo(U)
{
}
So the reason cannot be that function templates can only be fully specialized, because the third example is not a full specialization (the template parameter U
is still there), and yet it works.
Function templates can only be specialised completely, not partially.
You're using the fact that member functions of class templates are themselves function templates, so this rule still applies.
As for your edit: The following things can be explicitly (i.e. completely) specialized, from 14.7.3/1:
An explicit specialization of any of the following:
— function template
— class template
— member function of a class template
— static data member of a class template
— member class of a class template
— member enumeration of a class template
— member class template of a class or class template
— member function template of a class or class template
can be declared by a declaration introduced by
template<>;
I've emphasized the two statements that apply to your case. Absent any other explicit provisions, those entities can not be specialized partially.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With