I was thinking about how to create a list of all class which derive from a template base class.
First I want to have a template Base class:
template <typename T>
class Base
{
public:
Base() {};
virtual ~Base() {};
};
and a class which inherits from the base template class:
class Foo : public Base<Foo >
{
public:
Foo () {};
virtual ~Foo () {};
};
There might be any number of other subclasses like Foo
. The result should look like something:
std::vector<std::string> allTemplates = Base<Base>::getAllTemplateClasses();
My question is if its possible to create an list of all sub-classes at compile time? The Magic should happed in the Base class or with a really little effort in the child class.
I was thinking in different directions before. First I thought it is possible to use constexpr
. Like every child class needs a static function with the signature:
constexpr static std::string name() { "Foo";}
Or thought maybe it is possible with meta programming and create a compile time list like the Example of A Compile Time Data Structure Using,Template-Meta. The problem here is that I don't know the head of for the template creation.
Next I was thinking about to use macros and build an enum struct like this way Enum structs expanding. So I can't find any solution for this problem I want to ask you if its even possible?
Edit:
To say it clear: I want to have a list of the child objects, without any need to create them.