Template hierarchy question

I

Inso Reiges

Hi,
I`m not a native english speaker, so please bear with me.

I`ve inherited a template library codebase. This library is using a
mixin approach and passes a concrete class type down the inheritance
tree in template parameter, as such:

template <class T> class Base {};
template <class T> class Der1 : public Base<T> {};
.....
template <class T> class DerN : public DerN_minus_1<T> {};

The given hierarchy is simplified and uses multiple inheritance in
real code.

I have to make concrete (non-templated) classes on top of this
hierarchy:

class C1 : public DerN<C1> {};
class C2 : public DerN<C2> {};
....

Now, i have a collector class, that has to register all those C
classes somewhere inside it, like this:

class Collector { private: std::list<SomeType> mylist; };

But, as you know, i can not use templates for SomeType, because
compiler needs a concrete type. But i have to register C classes in a
single collection and C classes have to be on top of the template
inheritance hierarchy.

Could you please advise a solution?

Thank you, Inso Reiges.
 
G

gnuyuva

Hi,
I`m not a native english speaker, so please bear with me.

I`ve inherited a template library codebase. This library is using a
mixin approach and passes a concrete class type down the inheritance
tree in template parameter, as such:

template <class T> class Base {};
template <class T> class Der1 : public Base<T> {};
....
template <class T> class DerN : public DerN_minus_1<T> {};

The given hierarchy is simplified and uses multiple inheritance in
real code.

I have to make concrete (non-templated) classes on top of this
hierarchy:

class C1 : public DerN<C1> {};
class C2 : public DerN<C2> {};
...

Now, i have a collector class, that has to register all those C
classes somewhere inside it, like this:

class Collector { private: std::list<SomeType> mylist; };

But, as you know, i can not use templates for SomeType, because
compiler needs a concrete type. But i have to register C classes in a
single collection and C classes have to be on top of the template
inheritance hierarchy.

use
std::list<Base*> base_list;
You need to store pointers then.
 
J

James Kanze

I`m not a native english speaker, so please bear with me.
I`ve inherited a template library codebase. This library is
using a mixin approach and passes a concrete class type down
the inheritance tree in template parameter, as such:
template <class T> class Base {};
template <class T> class Der1 : public Base<T> {};
....
template <class T> class DerN : public DerN_minus_1<T> {};
The given hierarchy is simplified and uses multiple inheritance in
real code.
I have to make concrete (non-templated) classes on top of this
hierarchy:
class C1 : public DerN<C1> {};
class C2 : public DerN<C2> {};
...
Now, i have a collector class, that has to register all those C
classes somewhere inside it, like this:
class Collector { private: std::list<SomeType> mylist; };
But, as you know, i can not use templates for SomeType,
because compiler needs a concrete type. But i have to register
C classes in a single collection and C classes have to be on
top of the template inheritance hierarchy.
Could you please advise a solution?

You can't do it directly, since Base isn't a class. You have
two basic possible solutions:

-- use something like boost::any, or

-- create an artificial polymorphic base class, multiply
inherit from it in your concrete classes, and maintain a
list of pointers to it, e.g.:

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

class C1 : public DerN<C1>, public Listable {} ;
class C2 : public DerN<C2>, public Listable {} ;
// ...

std::list< Listable* > myList ;

This means, of course, that you'll have to handle lifetime
issues yourself, however.
 
G

gnuyuva

You can't do it directly, since Base isn't a class. You have
two basic possible solutions:

My bad, I completely overlooked Base class. Thanks for pointing it
out.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top