News for hackers. Stuff that matters.
Re:Don’t blindly prefer emplace_back to push_back by xKingfisher on Thursday March 04, 2021 @05:29AM (#26340785)
The article did touch on a possible reason for the misunderstanding.

If you already know from previous versions that push_back makes a copy and that C++11 added move semantics and emplace_back, it's not a huge leap to connect the two. Especially if you don't notice push_back's rvalue overload (or understand rvalues).

And if you're new to C++ and are having all of this thrown at you at once it'd definitely be easy to get some crossed wires.

Re:Don’t blindly prefer emplace_back to push_back by brown9-2 on Thursday March 04, 2021 @07:10AM (#26341375)
As someone new to C++, it seems endlessly confusing that you are supposed to know if a function call is making a copy vs a move by figuring out the signature of the function being called or knowing which one the compiler picks, rather than being explicit about it in the code (with std::move or somehow annotating the call with what you want).
Re:Don’t blindly prefer emplace_back to push_back by junon on Thursday March 04, 2021 @08:24AM (#26342005)
You should always know the signature of a function you're calling. Please be reading docs.

The part I agree with are the method selection semantics. A good rule of thumb (though don't rely on it) is that the "more specific" prototype generally wins.

Re:Don’t blindly prefer emplace_back to push_back by brown9-2 on Thursday March 04, 2021 @10:47AM (#26343847)
I agree you should know what you are calling when writing code, but there is also a need for people to read the code and understand what the behavior will be. My impression is that C++ relies a lot on implicit knowledge.
Re:Don’t blindly prefer emplace_back to push_back by vlovich123 on Thursday March 04, 2021 @11:23AM (#26344364)
All languages do.
Re:Don’t blindly prefer emplace_back to push_back by Yoric on Thursday March 04, 2021 @02:14PM (#26346791)
Well, many languages try to keep this minimal. C++ doesn't.
Re:Don’t blindly prefer emplace_back to push_back by ziml77 on Thursday March 04, 2021 @04:49PM (#26348894)
And it's nothing new with move/copy semantics either. Even passing by reference is bad for hiding that. Every time I write C++ I'm tempted to take pointers as parameters in places where you'd normally take a reference just so the call site has an indication of what I'm doing.
Re:Don’t blindly prefer emplace_back to push_back by junon on Thursday March 04, 2021 @11:55PM (#26352898)
Totally agree.
Re:Don’t blindly prefer emplace_back to push_back by xKingfisher on Thursday March 04, 2021 @07:23AM (#26341456)
It's definitely a lot to process.

I found these two articles really helpful when trying to understand moves:

https://abseil.io/tips/55 https://abseil.io/tips/77

Though I'd also say don't worry about it too much, especially at first. If you're copying a lot of temporary objects moving can get you some performance wins, but that's something profiling should be telling you m

Re:Don’t blindly prefer emplace_back to push_back by jeffbee on Thursday March 04, 2021 @11:57AM (#26344848)
Authors should also try to be aware that for some types there's not a meaningful difference between copy construction and move construction.
Re:Don’t blindly prefer emplace_back to push_back by gpderetta on Thursday March 04, 2021 @01:37PM (#26346289)
Well ideally functions shouldn't be moving behind your back, i.e if you write:

  foo(someValue);
someValue will bever be implicitly moved. Either you need an explicit cast (i.e. std move) or someValue is actually an expression returing a temporary or an rvalue.

Of course if foo takes someValue by non const reference it might still modify someValue as it wishes (and moving out of it is just one possible mutation). But this is not a specific issue with moves but with functions taking non const references in general, which should be used only sporadically and hopefully the name makes the mutation clear.