Mircea Ispas
Mircea Ispas

Reputation: 20780

Check if type uses multiple inheritance or not

I have these classes:

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

I want to make a template function like this:

template<typename T>
void f(T* arg);

And to make 2 specializations for it:

Let's say f_specialization1() is for first case and f_specialization2() (I know hey must have same name, this is just a convenient convention). I want this behavior:

A a;
B b;
C c;
D d;

f(&a);//here f_specialization1 is called
f(&b);//here f_specialization1 is called
f(&c);//here f_specialization1 is called
f(&d);//here f_specialization2 is called

Is it possible to achieve this behavior?

Upvotes: 1

Views: 183

Answers (1)

statueuphemism
statueuphemism

Reputation: 664

While I do not know your intent and agree that the intended behavior should be included in the question to receive better design solutions, a simple mechanism to achieve what you request is to create a blank interface that all of your multiply-inherited classes must also inherit from and to specialize for that MultipleInheritance "interface".

class MultipleInheritance
{
protected:
    MultipleInheritance() {}
};


class A{};
class B{};
class C : public A{};
class D : public A, public B, public MultipleInheritance {};

template<>
void f(MultipleInheritance* arg);

template<typename T>
void f(T* arg);

However, I personally think think this would be very much begging for a redesign at this point depending on the design constraints and it really does not allow you to do much useful since an otherwise useless type is used to select this function specialization.

Upvotes: 1

Related Questions