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
I would suggest you to remove the following implementation from your code altogether, so that compiler can generate error at compile-time itself if T is not int . Early detection of error is better than delay detection(which is done at runtime).
template<typename T>
T MyClass::GetTFromVariable(shared_ptr<TOtSimpleVariable> v, string s)
{
throw std::runtime_error("Don't know how to convert " + ToString(v->GetString()));
}
There is exactly similar topic/issue discussing this thing. Please have a look at this:
Partial template specialization for specific type, c++
The full specialization is no longer a template. It's a concrete function. As such it needs to be (implicitly or explicitly) declared inline
. The easiest is to add that keyword before the return type specification.
That said, the presented code does not correspond to your error message.
Your error message talks about return type std::string
, not return type int
.
Cheers & hth.,
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<int>(shared_ptr<TOtSimpleVariable> v, string s);
and in the implementation have:
template<>
int MyClass::GetTFromVariable<int>(shared_ptr<TOtSimpleVariable> 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.