Take the 2-minute tour ×

How can I force a template parameter T to be a subclass of a specific class Baseclass? Something like this:

template <class T : Baseclass> void function(){
    T *object = new T();

share|improve this question
What are you trying to accomplish by doing this? –  sth Jul 4 '10 at 15:45
I just want to make sure that T is actually an instance of a subclass or the class itself. The code inside the function that I have provided is pretty much irrelevant. –  phant0m Jul 4 '10 at 15:58
on the contrary, it's very relevant. It determines whether it's a good idea or not to put work into that test. In many (all?) cases, there is absolutely no need to enforce such constrains yourself, but rather let the compiler do it when instantiating. For example, for the accepted answer, it would be good to put a check on whether T derived from Baseclass. As of now, that check is implicit, and is not visible to overload resolution. But if nowhere such an implicit constraint is done, there appears to be no reason for an artificial restriction. –  Johannes Schaub - litb Jul 4 '10 at 16:15
Yes, I agree. However, I just wanted to know whether there is a way to accomplish this or not :) But of course, you have a very valid point and thanks for the insight. –  phant0m Jul 4 '10 at 16:27

6 Answers 6

up vote 24 down vote accepted

In this case you can do:

template <class T> void function(){
    Baseclass *object = new T();


This will not compile if T is not a subclass of Baseclass (or T is Baseclass).

share|improve this answer
ah yes, that's a good idea. thanks! I take it then there's no way of defining it in the template definition? –  phant0m Jul 4 '10 at 15:55
@phant0m: Correct. You can't explicitly constrain template parameters (except using concepts, which were considered for c++0x but then dropped). All constraints happen implicitly by the operations you perform on it (or in other words the only constraint is "The type must support all operations that are performed on it"). –  sepp2k Jul 4 '10 at 16:00
ah ic. Thanks a lot for the clarification! –  phant0m Jul 4 '10 at 16:04
That executes the T() constructor, and requires the existence of the T() constructor. See my answer for a way that avoids those requirements. –  Douglas Leeder Jul 5 '10 at 9:15
Nice and clear, but this is a problem if T is a "heavy" class. –  David Lively Jan 8 at 20:32

To execute less useless code at runtime you can look at: http://www.stroustrup.com/bs_faq2.html#constraints which provides some classes that perform the compile time test efficiently, and produce nicer error messages.

In particular:

template<class T, class B> struct Derived_from {
        static void constraints(T* p) { B* pb = p; }
        Derived_from() { void(*p)(T*) = constraints; }

template<class T> void function() {
share|improve this answer
thanks for the link! –  phant0m Jul 5 '10 at 9:58
For me, this is the best and most interesting answer. Be sure to check Stroustrup's FAQ to read more about all kinds of constraints you could enforce similarly to this. –  Jean-Philippe Pellet Mar 8 '13 at 23:32
Indeed, this is one hell of an answer! Thanks. The site mentioned is moved here: stroustrup.com/bs_faq2.html#constraints –  Jan Korous Apr 3 '13 at 13:51
@JanKorous Thanks. I've updated the link. –  Douglas Leeder Apr 3 '13 at 14:48

With a C++11 compliant compiler, you can do something like this:

template<class Derived> class MyClass {

    MyClass() {
        // Compile-time sanity check
        static_assert(std::is_base_of<BaseClass, Derived>::value, "Derived not derived from BaseClass");

        // Do other construction related stuff...

I've tested this out using the gcc 4.8.1 compiler inside a CYGWIN environment - so it should work in *nix environments as well.

share|improve this answer

You could use Boost Concept Check's BOOST_CONCEPT_REQUIRES:

#include <boost/concept_check.hpp>
#include <boost/concept/requires.hpp>

template <class T>
    ((boost::Convertible<T, BaseClass>)),
(void)) function()
share|improve this answer
thanks for the suggestion. I will check it out. –  phant0m Jul 4 '10 at 16:07

You don't need concepts, but you can use SFINAE:

template <typename T>
boost::enable_if< boost::is_base_of<Base,T>::value >::type function() {
   // This function will only be considered by the compiler if
   // T actualy derived from Base

Note that this will instantiate the function only when the condition is met, but it will not provide a sensible error if the condition is not met.

share|improve this answer
What if you wrap all functions like this? btw what does it return? –  the_drow Jul 4 '10 at 20:51
The enable_if takes a second type parameter that defaults to void. The expression enable_if< true, int >::type represents the type int. I cannot really understand what your first question is, you can use SFINAE for whatever you wish, but I don't quite understand what you intend to do with this over all functions. –  David Rodríguez - dribeas Jul 4 '10 at 21:36

By calling functions inside your template that exist in the base class.

If you try and instantiate your template with a type that does not have access to this function, you will receive a compile-time error.

share|improve this answer
This does not ensure that T is a BaseClass because the declared members in BaseClass could be repeated in the declaration of T. –  Daniel Trebbien Jul 4 '10 at 16:11

Your Answer


