samedi 6 février 2021

Saving types of templated classes

Consider these simplified classes (Interface, and templated Derived class):

class Interface
{
    virtual ~Interface() = default;
};

template<typename... T>
class Derived : public Interface
{
    std::tuple<T...> member;
};

And then I push arbitrary templated types of Derived class into data vector that holds Interface pointer.

std::vector<Interface*> data;
push_back(new Derived<int, float>{});
push_back(new Derived<float, float>{});
push_back(new Derived<double, char>{});

My question is how could I get template type from the Interface* without knowing what type it is (since I do not know what type it is I also can not cast it to the proper type). Preferably I would like to associate each index of vector with the types used in the template.

I tried storing typedef/alias for each derived class, but I could not find a way to accomplish this. Would that be even possible to achieve, since classes/types are compile time concept?

Aucun commentaire:

Enregistrer un commentaire