9

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);

?

vladon
  • 8,158
  • 2
  • 47
  • 91
  • 1
    Works fine with me http://coliru.stacked-crooked.com/a/7c54feda67017bf1 (I had to add a constructor to the struct but that's all) – Pumkko Jul 12 '16 at 07:56
  • 3
    No, both are [not allowed](http://rextester.com/WBNIK94302). If you provide a ctor (`S::S(int, int)`) then both are [fine](http://rextester.com/UHFEW17433). – songyuanyao Jul 12 '16 at 07:57
  • @Pumkko I meant exactly simple struct without ctor :) – vladon Jul 12 '16 at 08:09

3 Answers3

9

Actually neither work.

It was decided that emplace-style construct functions in C++ std would construct with ()s not {}s. There is no strong reason why this was chosen (that I know of).

emplace_alt snd make_unique_alt could be added to std where it constructs using {} instead. (a better name should be chosen, naturally)

So the short answer is "because std says so". The medium answer is "it is a near arbitrary choice made by std, followed elsewhere to be consistent". The long answer would involve being in the room where it happened and where it was revisited: this is not a long answer.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
5

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.

paweldac
  • 1,144
  • 6
  • 11
1

how about

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

without defining an extra ctor with args.

rnd_nr_gen
  • 2,203
  • 3
  • 36
  • 55