views:

303

answers:

6

Let's say I have the classes:

class Base{};

class A: public Base{
    int i;
};

class B:public Base{
    bool b;
};

And now I want to define a templated class:

template < typename T1, typename T2 >
class BasePair{
    T1 first;
    T2 second;
};

But I want to define it such that only decendants of class Base can be used as templateparameters.

How can I do that?

+3  A: 

C++ doesn’t yet allow this directly. You can realize it indirectly by using a STATIC_ASSERT and type checking inside the class:

template < typename T1, typename T2 >
class BasePair{
    BOOST_STATIC_ASSERT(boost::is_base_of<Base, T1>);
    BOOST_STATIC_ASSERT(boost::is_base_of<Base, T2>);
    T1 first;
    T2 second;
};
Konrad Rudolph
i don't want the parameters just to have the same base class - i want them to have my specific class Base as baseclass
Mat
@Math – oops, wrong type check. Was still tired.
Konrad Rudolph
A: 

First, fix the declaration

template < class T1, class T2 >
class BasePair{
    T1 first;
    T2 second;
};

Then, you can declare in a base class some private function Foo(); and tell Base class to have BasePair as friend; then in friend constructor you just have to call this function. This way you will get compile-time error when someone tries to use other classes as template parameters.

alemjerus
“fix the declaration” – the same goes for you, too. ;-)
Konrad Rudolph
A: 
class B
{
};
class D : public B
{
};
class U
{
};

template <class X, class Y> class P
{
    X x;
    Y y;
public:
    P()
    {
        (void)static_cast<X*>((Y*)0);
    }
};
explain please.. how does this guarantee that the template parameters are decendants of B?
Mat
When you instantiate the template P<B, D> ok; the compiler will try using the static_cast template function to convert D type pointer to B type pointer. This will work fine but, in case of P<B, U> nok; this conversion fails and you will receive a compiler error (error C2440: 'static_cast' : cannot convert from 'U *' to 'B*'). Moreover this won't have any run-time effect(code is not slower). It is a static or compile-time type checking.
+2  A: 

More exactly:

class B {};
class D1 : public B {};
class D2 : public B {};
class U {};

template <class X, class Y> class P {
    X x;
    Y y;
public:
    P() {
        (void)static_cast<B*>((X*)0);
        (void)static_cast<B*>((Y*)0);
    }
};

int main() {
    P<D1, D2> ok;
    P<U, U> nok; //error
}
+1  A: 

This was a great question! While researching this via this link, I came up with the following, which admittedly isn't much different than the provided solution there. Learn something everyday...check!

#include <iostream>
#include <string>
#include <boost/static_assert.hpp>

using namespace std;

template<typename D, typename B>
class IsDerivedFrom
{
  class No { };
  class Yes { No no[3]; };

  static Yes Test(B*); // declared, but not defined
  static No Test(...); // declared, but not defined

public:
  enum { IsDerived = sizeof(Test(static_cast<D*>(0))) == sizeof(Yes) };
};


class Base
{
public:
    virtual ~Base() {};
};

class A : public Base
{
    int i;
};

class B : public Base
{
    bool b;
};

class C
{
    string z;
};


template <class T1, class T2>
class BasePair
{
public:
    BasePair(T1 first, T2 second)
        :m_first(first),
         m_second(second)
    {
        typedef IsDerivedFrom<T1, Base> testFirst;
        typedef IsDerivedFrom<T2, Base> testSecond;

        // Compile time check do...
        BOOST_STATIC_ASSERT(testFirst::IsDerived == true);
        BOOST_STATIC_ASSERT(testSecond::IsDerived == true);

        // For runtime check do..
        if (!testFirst::IsDerived)
            cout << "\tFirst is NOT Derived!\n";
        if (!testSecond::IsDerived)
            cout << "\tSecond is NOT derived!\n";

    }

private:
    T1 m_first;
    T2 m_second;
};


int main(int argc, char *argv[])
{
    A a;
    B b;
    C c;

    cout << "Creating GOOD pair\n";
    BasePair<A, B> good(a, b);

    cout << "Creating BAD pair\n";
    BasePair<C, B> bad(c, b);
    return 1;
}
RC
A: 

In the answer suggested by unknown(yahoo), it is not required to actually have the variables of type X and Y as members. These lines are sufficient in the constructor:

static_cast<B*>((X*)0);
static_cast<B*>((Y*)0);
hype