jeudi 20 avril 2017

Variadic template expansion, inheritance, and std::unique_ptr

I have code that works like this:

#include <memory>
#include <vector>

using namespace std;

struct A {
    virtual ~A() = default;
};

struct B : public A {
};

template<typename... Ts> struct C {
    C() : v_({new Ts...}) {}

    ...

    std::vector<A*> v_;
};

...

C<B, B, A> bba;

I'd like to use std::unique_ptr and std::make_unique to avoid calling new explicitly and iterating over v_ to delete it in destructor (v_ will become std::vector<std::unique_ptr<A>>) but can't figure out how to marry up std::make_unique with initialization list and variadic expansion (I suspect due to std::unique_ptr being move-only). Any suggestions?

Aucun commentaire:

Enregistrer un commentaire