0

I have a polymorphic template MyClass that takes argument T.

template<class T>
class MyClass {...};

I would like to force an inheritance without specifing it in every derieved class, something like:

template<class T, class U : public T>
class MyClass<U> : public MyClass<T>;

Is there a way to do it?

EDIT: For Example If I have

class Base {...};
class Derieved : public Base {...};

Then if the compiler creates MyClass<Derieved> then it would add

class MyClass<Derieved> : public MyClass<Base>

without specifing it every time I use MyClass with inheritance.

I want to be able to create MyClass with any parameter, but when the parameter inherits from some other class, so will the template.

Omer Rosler
  • 237
  • 1
  • 10
  • Huh? What do you want to inherit from what? Can you provide an example? – Barry Jun 12 '15 at 23:52
  • http://stackoverflow.com/questions/122316/template-constraints-c not tried it, but `static_assert` mixed with `std::is_base_of seems` like the best option. – Nathan Cooper Jun 12 '15 at 23:56
  • The problem isn't only to make the compiler check if there is an inheritance, but to create an inheritance based on it. – Omer Rosler Jun 13 '15 at 00:09

1 Answers1

0

You want to automatically retrieve the base classes of an object. Sadly, it won't be possible before at least C++17, when compile-time reflection is integrated.

Quentin
  • 62,093
  • 7
  • 131
  • 191