optional inhertiance with templates

S

skscpp

I have a question regarding "optional" inheritance in regards to template.
This is not compiled code.

template<typename T, typename P1 = Invalid<1>, typename P2 = Invalid<2>,
typename P3 = Invalid<3> >
class Mgr : public T,
public virtual Mgmt<P1>,
public virtual Mgmt<P2>,
public virtual Mgmt<P3>
{
public:
// .....
};

Now if the user sends the following:
class Mgr1;
class My1;
class My2;

Mgr<Mgr1, My1> MyMgr1; // I want the Mgr template to inherit from Mgr1 and
Mgmt<My1>, not Mgmt<Invalid<2>> or
Mgmt<Invalid<3>>

Mgr<Mgr1, My1, My2> MyMgr2; // I want the Mgr template to inherit from Mgr1,
Mgmt<My1>, and Mgmt<My2>, not Mgmt<Invalid<3> >

Is this possible to that?

Thanks.
 
R

Rob Williscroft

skscpp wrote in
I have a question regarding "optional" inheritance in regards to
template. This is not compiled code.

template<typename T, typename P1 = Invalid<1>, typename P2 =
Invalid<2>, typename P3 = Invalid<3> >
class Mgr : public T,
public virtual Mgmt<P1>,
public virtual Mgmt<P2>,
public virtual Mgmt<P3>
{
public:
// .....
};

Now if the user sends the following:
class Mgr1;
class My1;
class My2;

Mgr<Mgr1, My1> MyMgr1; // I want the Mgr template to inherit from Mgr1
and Mgmt<My1>, not Mgmt<Invalid<2>> or
Mgmt<Invalid<3>>

Mgr<Mgr1, My1, My2> MyMgr2; // I want the Mgr template to inherit from
Mgr1, Mgmt<My1>, and Mgmt<My2>, not Mgmt<Invalid<3> >

Is this possible to that?

Typed into newsreader ...

struct invalid {};

template<
typename T,
typename P1 = invalid,
typename P2 = invalid ,
typename P3 = invalidstruct manage_base : T, P1, P2, P3
{
};

template<
typename Tstruct manage_base< T, invalid, invalid, invalid > : T
{
};

template<
typename T, typename P2struct manage_base< T, P2, invalid, invalid > : T, P2
{
};

// etc ...

template<
typename T,
typename P1 = invalid,
typename P2 = invalid,
typename P3 = invalidclass Mgr : public manage_base< T, P1, P2, P3 >
{
// some code ??
};

If for some reason you want to have Mgr inherit directly from
T, P1 ... then you'll have to specialize Mgr directly.

HTH

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

templates?? 2
nested templates and partial specialization 8
templates 10
optional parameter in a template 3
Error when using std::less 2
templates 2
Templates and g++ 4
Templates 4 a Nu-B 1

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top