template specialization multiply defined symbols

前端 未结 3 1364
日久生厌
日久生厌 2020-12-29 23:56

I know i\'m missing something easy here but I\'ve got a templated member function of a class which I\'ve specialised.

MyClass
{
    template

        
3条回答
  •  长情又很酷
    2020-12-30 00:30

    As Alf noted, the full specialization is no longer a template. However, I am not sure it has to be defined inline. You should also be able to split the declaration and definition.

    I.e. In your header have:

    template<> 
    int MyClass::GetTFromVariable(shared_ptr v, string s);
    

    and in the implementation have:

    template<>
    int MyClass::GetTFromVariable(shared_ptr v, string s)
    {
        return v->GetInteger();
    }
    

    I also had thought that by rights the templated definition should also be explicitly inline (I always have done so) but would not be too surprised if a given compiler was lax in applying the ODR for templates. I'd be interested to see the standard reference that states otherwise.

提交回复
热议问题