This question already has an answer here:
I have a templated function which I want to have two paths that it can follow: (very simplified for obvious reasons)
// Implementation A
template <class T>
T getValue( uint8_t id )
{
return dynamic_cast<T>( createClass( id ) );
}
// Implementation B
template <class T>
T getValue( uint8_t id )
{
return id + 1;
}
In this usage of example above, I have a class MyClass, and then a class MySubClass : public MyClass
I then want to be able to call any of the following:
getValue<MyClass*>( 3 ) // calls Implementation A
getValue<MySubClass*>( 4 ) // calls Implementation A
getValue<int>( 5 ) // calls Implementation B
getValue<float>( 6 ) // calls Implementation B
Now I know that I can directly specialize the template with MyClass*, but that wouldn't help for MySubClass*
I've done some googling for this, but haven't found an answer that seems to quite give me the push I'm looking for.
I've got as far as:
template <typename T>
typename std::enable_if<std::is_base_of<NvPacket*, T>::value, T>::type
getValue( uint8_t id )
{
return dynamic_cast<T>( createClass( id ) );
}
template <typename T>
typename std::enable_if<!std::is_base_of<NvPacket*, T>::value, T>::type
getValue( uint8_t id )
{
return id + 1;
}
But when I go to compile this with the following being called:
getValue<uint8_t>();
I get a compiler error of:
ambiguous call to overloaded function
Aucun commentaire:
Enregistrer un commentaire