Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force template <class> to be derived from BaseClassA?

Is there any possibility to force a template to be from a certain base class so that I can call the base class function?

template <class T>
void SomeManager::Add(T)
{
    T->CallTsBaseClassFunction();
    //... do other stuff
}
like image 624
cppanda Avatar asked Feb 13 '11 13:02

cppanda


3 Answers

Sure, you can combine type traits with SFINAE:

#include <type_traits>

template <class T>
typename std::enable_if<std::is_base_of<your_base_class, T>::value, void>::type
SomeManager::Add(T)
{
    T->CallTsBaseClassFunction();
    //... do other stuff
}

Although I don't really see the benefit here.

like image 134
fredoverflow Avatar answered Oct 15 '22 19:10

fredoverflow


The easiest solution is to add a snippet of code that compiles only if it's what you expected:

template <class T>
void SomeManager::Add(T t)
{
    assert((Base const*)&t); // T must inherit from Base to allow T*->Base* conversion.
    t.CallTsBaseClassFunction();
    //... do other stuff
}
like image 37
MSalters Avatar answered Oct 15 '22 20:10

MSalters


Worth to mention that it can be done at compile time in a more readable fashion with static_assert. Something in the lines of:

class Base {};

template<class B>
class Template{
    static_assert(std::is_base_of<Base, B>::value, "B must derive from nmspc::Base");
}

It works even when B is exactly Base. If Base is itself a templated class it becomes more complicated but it can still be done and there's plenty of resources online.

like image 20
NicoBerrogorry Avatar answered Oct 15 '22 21:10

NicoBerrogorry