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.