I'd like my template function to accept as an argument only a class that inherits from a base class. I think a code snippet explains it better.
class Base
{
// Some magic
}
class Derived : public Base
{
// Even more magic
}
class Foo
{}
// Is it possible to tell template to accept only classes derived from Base?
template<class T>
do_something(T obj)
{
// Perform some dark magic incantations on obj
}
int main()
{
Foo foo;
Derived derived;
do_something<Derived>(derived); // Normal
do_something<Foo>(foo); // Compilation error if I understand templates correctly
}