web-dev-qa-db-ja.com

GCCエラー:名前空間以外のスコープでの明示的な特殊化

次のコードを移植しようとしています。標準ではnamescape以外のスコープでの明示的な特殊化が許可されていないため、オーバーロードを使用する必要がありますが、この特定の場合にこの手法を適用する方法が見つかりません。

class VarData
{
public:
    template < typename T > bool IsTypeOf (int index) const
    {
        return IsTypeOf_f<T>::IsTypeOf(this, index); // no error...
    }

    template <> bool IsTypeOf < int > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
    {
        return false;
    }

    template <> bool IsTypeOf < double > (int index) const // error: explicit specialization in non-namespace scope 'class StateData'
    {
        return false;
    }
};
27
Ryan

メンバーテンプレートの特殊化をクラス本体の外に移動するだけです。

class VarData
{
public:
    template < typename T > bool IsTypeOf (int index) const
    {
        return IsTypeOf_f<T>::IsTypeOf(this, index);
    }
};

template <> bool VarData::IsTypeOf < int > (int index) const
{
    return false;
}

template <> bool VarData::IsTypeOf < double > (int index) const
{
    return false;
}
40
CB Bailey

クラス外のスペシャライゼーションを次のように定義します。

template <> 
bool VarData::IsTypeOf < int > (int index) const 
{  //^^^^^^^^^ don't forget this! 
     return false;
}

template <> 
bool VarData::IsTypeOf < double > (int index) const 
{   //^^^^^^^ don't forget this!
     return false;
}
6
Nawaz