27

I have a template class, what I want to do are the following

  1. Make sure that an object is instantiated only if the template parameter passed is a subtype of a desired type
  2. Communicate to the user of the code upfront what is it that the template parameter must satisfy

(1) is sort of taken care automatically in the sense if the template parameter passed does not support some feature that the class uses the code will not compile. But this error may be detected fairly late. I want the checks to be as early as possible. What I also want to accomplish is that it should be obvious rightaway that template parameter that is passed has to be derived from a base type that I provide.

First, is this misguided ? and if not how shall I do this ? (the simplest way please, C++ is still new to me)

Thanks stackoverflow, you have really speeded up my C++ learning rate.

san
  • 4,144
  • 6
  • 32
  • 50

3 Answers3

45

Given some complete type MyBase, the following will yield a compile-time error if T is not derived from MyBase:

#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>

template<typename T>
class Foo {
    BOOST_STATIC_ASSERT_MSG(
        (boost::is_base_of<MyBase, T>::value),
        "T must be a descendant of MyBase"
    );
    // Foo implementation as normal
};

If you're using a C++03 compiler with TR1, you can use std::tr1::is_base_of instead of boost::is_base_of; if you're using a C++11 compiler, you can use std::is_base_of instead of boost::is_base_of and the static_assert keyword instead of the BOOST_STATIC_ASSERT_MSG macro:

#include <type_traits>

template<typename T>
class Foo {
    static_assert(
        std::is_base_of<MyBase, T>::value, 
        "T must be a descendant of MyBase"
    );
    // Foo implementation as normal
};

N.b. this will yield true_type for privately and ambiguously-derived types, so this is insufficient if what you really need is to treat T as-a MyBase (in most contexts).

Doc links:
Boost.StaticAssert
Boost.TypeTraits

ildjarn
  • 62,044
  • 9
  • 127
  • 211
4

From "Modern C++ Design", chapter 2.7 ("Detecting Convertibility and Inheritance at Compile Time"): you can use a sizeof trick:

typedef char Small;
class Big { char dummy[2]; };

Small Test(Base1);
Big Test(...);

const bool isSubclassOfBase1 = sizeof(Test(Derived1())) == sizeof(Small);

It exploits the fact that sizeof(...) can figure out the type that the expression evaluates to and since the return values have different sizes, the == check evaluates to true or false. If Derived1 is indeed the base of Base1, Small Test(Base1); overload is chosen and isSubclassOfBase1 will be true.

Extending from that, you can encapsulate a check and do a static assertion to make it fail at compile time:

#include <boost/static_assert.hpp>

class A {};
class B: public A {};
class C {};

template<class Base, class Derived>
struct SubclassCheck
{
    typedef char Yes;
    class No { char dummy[2]; };

    static Yes Test(Base);
    static No Test(...);
    enum {
        Value = (sizeof(Test(*(Derived*)NULL)) == sizeof(Yes))
    };
};

#define CHECK_DERIVES(b,d)\
    BOOST_STATIC_ASSERT((SubclassCheck<b,d>::Value));

int
main()
{
    CHECK_DERIVES(A, B);
    // CHECK_DERIVES(A, C); // fails
}

You can use any other static assertion implementation, not necessarily Boost's.

Alex B
  • 82,554
  • 44
  • 203
  • 280
  • And how do you cause an error at compile time if it's not equal? Nice avatar btw, loved that game. – Seth Carnegie Aug 11 '11 at 03:05
  • 1
    The problem with this approach is that, it doesn't pass for `protected/private` inheritances. You can see [this question for `is_base_of`](http://stackoverflow.com/questions/2910979/how-is-base-of-works), to see how exactly it's implemented for all type of inheritances. – iammilind Aug 11 '11 at 05:34
0

Yes, this is taken care of automatically in that if the parameter doesn't support the things you do with it, a compilation error will result. Manually checking if a type is a subtype of another type can only (AFAIK) be done at runtime, which is far later than compile time. I don't know what you mean by that error being detected late, compile time is as early as it gets. Also, if everyone checked the type of their template parameters, the STL couldn't work with pointers as well as actual class-based iterators, and wouldn't be nearly as flexible.

As for letting your user know the requirements, simply provide them in the documentation.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • @san that breaks a lot of the applications and flexibility of templates. Why not just accept a `Base*` and exploit polymorphism instead of using templates? That seems more like what you're needing. – Seth Carnegie Aug 11 '11 at 03:06
  • I dont see why it would break other applications, (if that is obvious, bear with me, all this is new to me) I dont want all compilers to have this mandatory check. Just want to enable it (optionally) in one class. The `Base*` that I have in mind is just a class with a few static functions, I do not want to allocate memory for an actual object. – san Aug 11 '11 at 03:12
  • @san I mean it would break other applications of your functions, i.e. in how many places and ways they can be applied. Also, you don't have to allocate another object to cast a subclass pointer to a parent class pointer and use polymorphism. You really shouldn't be using templates for this. – Seth Carnegie Aug 11 '11 at 03:14
  • @san if you'll show me your template code, I'll show you how to convert it to non-template code using polymorphism. – Seth Carnegie Aug 11 '11 at 03:15
  • Oh Nice! I think if I stick to static or state free functions then I do not need to allocate memory and I can just work with `Base*` pointer. Is that right ? – san Aug 11 '11 at 04:03
  • 1
    @san oh, you're just using classes to group static functions? You can't access those through pointers, so you do need templates for that, but then it doesn't help you at all to check if a class inherits from another class at all, since static functions are not inherited. Perhaps you should be passing around function objects instead of using templates to call static class functions. – Seth Carnegie Aug 11 '11 at 04:50