tags:

views:

65

answers:

3

How can I accomplish what is suggested by the following?:

template<typename T>
class Base{...};
...
class Derived : public Base<int>{...};
+1  A: 

That looks good.

Ramónster
+1  A: 

There's a specific idiom associated with this called static inheritance.

template<typename T> class Base {
    void MyStaticVirtualFunction() { T::MSVF(); }
};
class Derived : public Base<Derived> {
    void MSVF();
};

In this case Base acts as a base class at compile-time and the function call is resolved statically, but the behaviour of Base can still vary.

DeadMG
I think it is more known as static polymorphism - see http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern#Static_polymorphism
Suma
@Suma: Close enough.
DeadMG
+5  A: 

Yes, it's legal.

Base is a class template, and when provided all its template parameters it can be instantiated, which makes it an instantiated class. So Base<int> is a class name, and you can inherit from it just fine.

GMan