2012-03-04
クラスのメンバ関数をテンプレート関数として作成し,さらにそれを特殊化する場合,特殊化は,特殊化宣言も含めてクラス外で行わなければならないらしい.
NG例 †
class TTest
{
public:
template<typename T> T one(void) const {return 1;}
template<> double one<double>(void) const {return 1.0;} // 特殊化
};
コンパイルすると,
15: error: explicit specialization in non-namespace scope ‘class TTest’ 15: error: template-id ‘one<double>’ in declaration of primary template
というエラーが出る(g++-4.4.4 の場合).
OK例 †
class TTest
{
public:
template<typename T> T one(void) const {return 1;}
};
template<> double TTest::one<double>(void) const {return 1.0;} // 特殊化
こう書けばOK.