Possible Duplicate:
“invalid use of incomplete type” error with partial template specialization成员函数的偏特
为什么我能做到这一点:
template <typename T>
struct A
{
void foo(int);
};
template <>
void A<int>::foo(int)
{
}
但不是这样的:
template <typename> struct C {};
template <typename T>
struct A
{
void foo(int);
};
template <typename T>
void A<C<T> >::foo(int)
{
}
对于第二种情况,GCC提供了以下错误:
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> >'
编辑:
当解释为什么第二个例子是不允许的,也请考虑使成员函数也是一个模板有没有效果上示例工作,哪些不是。也就是说,这仍然有效:
template <typename T>
struct A
{
template <typename U>
void foo(U);
};
template <>
template <typename U>
void A<int>::foo(U)
{
}
但这并不:
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)
{
}
这样的理由不能是函数模板只能是完全专用的,因为第三个例子是不是一个完整的专业化(模板参数U
仍然存在),但它的工作原理。
@Mankarse似乎是一个不同的问题。 –