template<typename T>class CConstraint{public: CConstraint() { } virtual ~CConstraint() { } template <typename TL> void Verify(int position, int constraints[]) { } template <> void Verify<int>(int, int[]) { }};在g ++下編譯它會產生以下錯誤:非命名空間范圍'class CConstraint'中的顯式特化在VC中,它編譯得很好。任何人都可以讓我知道解決方法嗎?
3 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
解決它的另一種方法是委托私有函數并重載該函數。這樣,您仍然可以訪問*this外部模板參數類型的成員數據。
template<typename T>
struct identity { typedef T type; };
template<typename T>
class CConstraint
{
public:
template <typename TL>
void Verify(int position, int constraints[])
{
Verify(position, constraints, identity<TL>());
}
private:
template<typename TL>
void Verify(int, int[], identity<TL>)
{
}
void Verify(int, int[], identity<int>)
{
}
};
- 3 回答
- 0 關注
- 682 瀏覽
添加回答
舉報
0/150
提交
取消