News for hackers. Stuff that matters.
Re:Don’t blindly prefer emplace_back to push_back by Koshkin on Thursday March 04, 2021 @07:44AM (#26341620)
> auto w = Widget(1,2,3);

Note that unlike in Java or C# this is not the right way to initialize a variable in C++.

Re:Don’t blindly prefer emplace_back to push_back by xdavidliu on Thursday March 04, 2021 @09:09AM (#26342638)
Herb Sutter offers another viewpoint:

In particular: #5 on that page: "we see that the right-hand style is not only more robust and maintainable for the reasons already given, but also arguably cleaner and more regular with the type consistently on the right when it is mentioned"

[1] https://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style...

Re:Don’t blindly prefer emplace_back to push_back by MaxBarraclough on Thursday March 04, 2021 @11:46AM (#26344710)
I don't think Herb's points apply to an ordinary declare-an-object-and-immediately-initialize-using-the-constructor, do they?

He gives an example using aggregate initialization, but that's not the same thing:

    // Classic C++ declaration order     // Modern C++ style

    employee e{ empid };                 auto e = employee{ empid };
    widget w{ 12, 34 };                  auto w = widget{ 12, 34 };
He makes the case that it's nicer to use auto pretty much wherever we can, to avoid repeating ourselves regarding type, and to avoid unexpected conversions. The auto keyword doesn't apply in our case, there's no way to use auto to simplify a statement like:

    Widget w(1,2,3);
Re:Don’t blindly prefer emplace_back to push_back by SloopJon on Thursday March 04, 2021 @01:45PM (#26346405)
Actually, he does discuss this in section 4b. He doesn't come out as strongly in favor of it, "The jury is still out on whether to recommend this one wholesale," although he does list what he considers some advantages. He also acknowledges in section 6 that it doesn't work for non-moveable types.

Despite my tendencies towards almost-always auto, I can't say that I consistently use auto x = type{init}, but there is certainly nothing wrong with it.

Re:Don’t blindly prefer emplace_back to push_back by gpderetta on Thursday March 04, 2021 @02:52PM (#26347292)
> He also acknowledges in section 6 that it doesn't work for non-moveable types.

Actually these days it does. Copy/Move elision is mandated by the language in this case and no valid copy/move constructor is required. Similarly is no possible to return non-movable non-copyable types from functions which opens the possibility of interesting code patternes.

Re:Don’t blindly prefer emplace_back to push_back by Koshkin on Thursday March 04, 2021 @11:56AM (#26344839)
Exactly. auto is only useful in contexts where the type is expected to be inferred (by the compiler).
Re:Don’t blindly prefer emplace_back to push_back by MaxBarraclough on Thursday March 04, 2021 @08:09AM (#26341854)
Great point, I'd missed that! It should be:

    Widget w(1,2,3);
Re:Don’t blindly prefer emplace_back to push_back by nec4b on Thursday March 04, 2021 @11:06AM (#26344117)
It is a completely valid way of initializing a variable in modern C++. Modern compilers will not create a copy assignment, if that is what you are afraid of.
Re:Don’t blindly prefer emplace_back to push_back by MaxBarraclough on Thursday March 04, 2021 @11:52AM (#26344771)
> Modern compilers will not create a copy assignment, if that is what you are afraid of.

Probably, but using the canonical syntax removes all doubt, following the C++ language specification. That's a small but real disadvantage in robustness when using the unnecessary assignment, and I'm not aware of any reason to favour using it.

Re:Don’t blindly prefer emplace_back to push_back by eklitzke on Thursday March 04, 2021 @02:16PM (#26346833)
Copy elision in this example is also required by the language specification (if you're using a modern version of C++).
Re:Don’t blindly prefer emplace_back to push_back by MaxBarraclough on Thursday March 04, 2021 @06:08PM (#26349804)
Neat, I didn't know that.

I still don't like it though. I'd rather express what I want to happen than express something I don't want to happen in the knowledge that the standard requires my compiler to be sufficiently smart to repair my statement. Consistent behaviour across different language versions is another plus.

If you'll permit me a moment's snark:

Most OOP languages: You can't copy objects. You may copy references to objects. Copying objects is one road to madness. Move semantics is another.

Old C++: You can copy objects, and specify your own copy constructors (which are permitted to side-effect), but the compiler is permitted to optimise away copy operations under certain circumstances. You shouldn't assume your copy constructor will be invoked whenever you see what looks like a copy.

New C++: You can copy objects, and specify your own copy constructors (which are permitted to side-effect), but the compiler is permitted to optimise away copy operations under certain circumstances, and beyond that, is now required to optimise away copy operations in some specific circumstances. You shouldn't assume your copy constructor will be invoked whenever you see what looks like a copy. When you see a copy operation in certain specific contexts, you are permitted to assume your copy constructor will not be invoked.

As with so many discussions of C++, I'm reminded of two quotes:

> Within C++, there is a much smaller and cleaner language struggling to get out.

- Bjarne Stroustrup [0]

> Do you know how the Orcs first came into being? They were elves once, taken by the dark powers, tortured and mutilated. A ruined and terrible form of life. And now... perfected.

- Saruman

[0] https://en.wikiquote.org/wiki/Bjarne_Stroustrup

Re:Don’t blindly prefer emplace_back to push_back by pragmatic8 on Thursday March 04, 2021 @09:14AM (#26342704)
Well, that piece of code is a perfectly valid way to initialize a variable in C++.