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似乎是一個不同的問題。 –