I have the factory that contains a map with the names of objects as key and pointer to function as value
template<class T>
static T* makeObj()
{
return new T();
}
using createFunction = std::function<void*()>;
map<std::string, createFunction> types;
template<class T>
static void add(const std::string& name)
{
types.insert(name, makeObj<T>);
}
I want to add an additional function to get a filtered list of types (get the list of inherited classes), something like this:
template<class baseType>
static std::list<std::string> getFilteredListOfTypes()
{
}
The question is how to do it.
The first idea is to create a map with struct as a value and store an object of the appropriate type there, I can use dynamic_cast and check all types, but I doubt that creating an object is a good idea.
Another way is to use std::is_base_of:
template< class Base, class Derived >
struct is_base_of;
is_base_of take two types, class Base I can get from my getFilteredListOfTypes, but how to get class Derived? Create an object and use decltype… but again here an object is created.
Is there a way to create such a filter function without creating objects?
Is it a bad idea to create an object here? Is it a bad idea to create an object for each item of map? I am a little scared of a situation where the factory will have hundreds of totally not small types.
Andrii is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.