c++unique-ptremplace

simple structs with make_unique and emplace_back


Given a struct:

struct S {
    int x;
    int y;
}

Why the Standard allows us to do this:

std::vector<S> vec;
vec.emplace_back(1, 2);

But does not allow to do this:

auto ptr = std::make_unique<S>(1, 2);

?


Solution

  • Please check your code.

    In cpp14 your example code doesn't compile: https://ideone.com/ewyHW6

    Both make_unique and emplace_back are using std::forward<Args>(args)... in background, so either both or none compiles.