mardi 21 avril 2020

How to expand variadic template class

Simplified I'm trying the following:

template<typename T>
struct SomeObject
{
    T value;
};

template<typename... Ts>
struct Container : public SomeObject<Ts>...
{
    void init()
    {
        (initObject<Ts>())...; // not working: how to execute initObject for each type
    }

    template<typename T>
    void initObject()
    {
        SomeObject<T>::value = new T();
        // do some other stuff
    }
};

class A {};
class B {};

int main()
{
    auto c = new Container<A, B>();
    c->init();
}

In my use case I cannot do the initialization in the constructor. So how can I get the init method to properly expand for all types?

Aucun commentaire:

Enregistrer un commentaire