vector’s emplace_back – vector as a constructor argument

I have the following structs:

struct A{};

struct B
    B(std::shared_ptr<A> a, int x): a_(a), x_(x){}

    std::shared_ptr<A> a_;
    int x_;

struct C
    C(std::vector<B> v, bool c){}

I would like to insert an object of type C to the vector but the following code doesn’t work:

std::vector<C> vecC;
vecC.emplace_back({std::make_shared<A>(), 2}, false);

Alternatively this way doesn’t make sense with emplace_back:

vecC.emplace_back(B{std::make_shared<A>(), 2}, false);

How should I insert an object of type C to the vector ?


You forgot another pair of braces for the std::vector. Also, you need to tell emplace_back() what kind of arguments you pass it, so you need to invoke std::vector‘s constructor:

vecC.emplace_back(std::vector{ B{ std::make_shared<A>(), 2 } }, false);

Alternatively, don’t use emplace_back() and use push_back() instead:

vecC.push_back({{{std::make_shared<A>(), 2}}, false});