我有一個是這樣的代碼:可變參數模板擴張,繼承和std ::的unique_ptr
#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;
我想使用std::unique_ptr
和std::make_unique
避免調用new
明確並遍歷v_
將其刪除在破壞者(v_
將成爲std::vector<std::unique_ptr<A>>
),但無法弄清楚如何結婚std::make_unique
與初始化列表和可變擴展(我懷疑由於std::unique_ptr
是移動只)。有什麼建議麼?
它改變了問題,但爲什麼'std :: vector>'而不是說'std :: tuple ...>'?如果預期集合會更改內容簽名,則構造函數應該是模板,而不是類。 –
TBBle