3

Possible Duplicate:
Is there a way to prevent a class from being derived from twice using a static assert and type trait?

What I'd like to prevent is more than one of the C based template from being derived in D (i.e. there should only ever be one instance of C derived from). Was hoping for maybe a static assert in C or B that may solve this.

// My Classes
template <class T>
class A {};

class B {};

template <class T, class S>
class C : public B, public virtual A<T> {};

// Someone elses code using my classes
class D : public C<Type1, Type2>, public C<Type3, Type4>
{
};
8
  • Can think of a couple runtime ways to prevent, but not compile time...
    – Jaime
    Jan 13, 2012 at 14:53
  • I can't tell if you want only one C at all, or only one C for a particular T and S.
    – Mark B
    Jan 13, 2012 at 14:56
  • @KerrekSB I wrote that and it was short of what I wanted. And now that its locked, I created this one to be more explicit
    – Jaime
    Jan 13, 2012 at 14:58
  • You explicitly derive D from two specializations of C, but you want to derive D from only one? I don't understand.
    – Beta
    Jan 13, 2012 at 14:59
  • 2
    @KerrekSB its not the same question. I asked the wrong thing.. The reason being is that in that example I asked for the assertion in the wrong place. Putting it in D does me know good, cause I don't write it. I need it in C or B, hence the new question. The original was not well thought out. I apologize for that.
    – Jaime
    Jan 13, 2012 at 15:03

1 Answer 1

5

As it stands, it's impossible for B or C to detect what else a more derived class inherits from, so you can't add an assertion there. However, by adding a "curiously recursive" template parameter, you can tell C what the derived class is. Unfortunately, this does require the derived class to give the correct template argument, and there's no way to enforce that.

You can then determine whether the derived class inherits from B in more than one way; it is a base class, but you can't convert a derived class pointer to B* (since that conversion is ambiguous). Note that this doesn't necessarily indicate multiple inheritance; the test will also fail if there's non-public inheritance.

So the best solution I can think of is:

#include <type_traits>

template <class T> class A {};
class B {};

template <class T, class S, class D>
class C : public B, public virtual A<T> {
public:
    C() {
        static_assert(
            std::is_base_of<C,D>::value && std::is_convertible<D*,B*>::value, 
            "Multiple inheritance of C");
    }
};

struct Type1 {};
struct Type2 {};
struct Type3 {};
struct Type4 {};

class Good : public C<Type1, Type2, Good> {};
class Evil : public C<Type1, Type2, Evil>, public C<Type3, Type4, Evil> {};

int main()
{
    Good good;
    Evil evil; // Causes assertion failure
}

I had to put the assertion in the constructor rather than the class definition, since some of the types are incomplete when the class template is instantiated. Unfortunately, this means that the error will only be reported for classes that are actually instantiated.

4
  • Thanks, I will try this out in a bit and respond accordingly
    – Jaime
    Jan 13, 2012 at 15:17
  • Ok, just got back to this. Very interesting how this works, its utilizing that fact that is_convertible doesn't work because the conversion is ambiguous. The only downside to this is that it produces a compile error and not a failed assertion, so I don't get my help message.
    – Jaime
    Jan 13, 2012 at 17:33
  • @Jaime: That depends on the quality of your compiler's type_traits implementation, which is rather variable at the moment. I get the expected assertion failure with gcc 4.6.1. Jan 13, 2012 at 17:38
  • Ok, I was wondering about that. I am using 4.5.1 so that makes sense. I will put a comment in to that affect. Thanks for your help.
    – Jaime
    Jan 13, 2012 at 17:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.