GCC error: explicit specialization in non-namespace scope

前端 未结 2 2113
灰色年华
灰色年华 2020-12-08 10:02

I am trying to port the following code. I know the standard doesn\'t allow explicit specialization in non-namescape scope and I should use overloading, but I just can\'t fin

相关标签:
2条回答
  • 2020-12-08 10:49

    Define the specialization outside the class as:

    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;
    }
    
    0 讨论(0)
  • 2020-12-08 10:56

    You just have to move your specializations of the member templates outside of the class body.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题