News for hackers. Stuff that matters.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by jcranmer on Wednesday July 10, 2019 @02:03PM (#20404590)
C++ has a few major flaws with respect to move and copy semantics. The biggest one is that copy semantics are default and silent: it requires less work to copy something than it does to use it by reference, and there is no visual indication if the value is being copied or accessed via reference. This means that it is way too easy to accidentally copy large objects (such as std::vector) without realizing you're copying them.

Most newer languages have realized that implicit copy semantics is usually a bad thing, and duplicating objects requires explicitly saying that you're duplicating them (such as calling a .clone() method). Of course, some types are value types, where copy is cheap and better than reference, but such mechanisms are opt-in. C++ automatically generates these mechanisms, and gives you nasty error messages when you opt-out of default copy.

Move semantics are almost always better, but in C++, with its historical baggage of opt-out-of-implicit-copy-semantics, it means that constructing move-only types requires a lot of excessive calls to std::move. Compilers do a good job of telling you when you put one too many calls to std::move in, but the code is definitely verbose compared to C++, to the point that it tends to strongly weigh against actually using C++'s ability to annotate move-only methods. Furthermore, without something like the mechanism in the blogpost here, compilers don't give any indication of API misuse, so you can't leverage move-only types to construct safe-by-construction APIs.

This is something I've been tripping over a lot recently, as I have a type system where calling most methods makes the original object unusable.

Re:Catching use-after-move C++ bugs with Clang's consumed an... by blub on Thursday July 11, 2019 @02:24AM (#20409524)
Copy semantics are not a flaw, they're a feature because they always work reliably.

If move semantics were the default, we'd either get more use-after-frees or require the awkwardness of the borrow checker to have a semblance of sanity.

Re:Catching use-after-move C++ bugs with Clang's consumed an... by rsp1984 on Wednesday July 10, 2019 @07:22PM (#20407616)
I appreciate your reply. But frankly, why would someone "accidentally" copy something by value?

I don't mean to sound arrogant but if someone is writing C++ at a level where he/she does accidental copies then that's a very clear sign to me to stay the hell away from move semantics and other advanced features.

I personally find things being copied by default a nice feature. It's more consistent than "PODs by value, objects by reference" such as in Java.

Re:Catching use-after-move C++ bugs with Clang's consumed an... by steveklabnik on Wednesday July 10, 2019 @07:58PM (#20407837)
> But frankly, why would someone "accidentally" copy something by value?

Well; https://groups.google.com/a/chromium.org/forum/#!msg/chromiu...

Re:Catching use-after-move C++ bugs with Clang's consumed an... by damnyou on Thursday July 11, 2019 @12:23AM (#20409111)
There is no such thing as a bad programmer, only bad tooling. C++ is bad tooling.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by patrick5415 on Thursday July 11, 2019 @03:20AM (#20409702)
A craftsman doesn’t blame his tools.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by damnyou on Thursday July 11, 2019 @04:03PM (#20415237)
A great craft recognizes how important good tools are.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by cozzyd on Thursday July 11, 2019 @01:33AM (#20409359)
The horrors I've seen in academic code speak otherwise.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by namirez on Wednesday July 10, 2019 @02:17PM (#20404728)
> The biggest one is that copy semantics are default and silent

This is not true. The rules are simple: rule of three, rule of five, or the recommended one, rule of zero [1]. If you define a copy constructor, the compiler won't generate a move constructor. If you define a move constructor explicitly, the compiler won't generate a copy constructor silently. You have total control over how the language behaves.

So in summary, either stick to the rule of 5 or the rule of zero and you won't be surprised. If you don't mind expensive copies, rule of 3 is sufficient.

[1] https://en.cppreference.com/w/cpp/language/rule_of_three

Re:Catching use-after-move C++ bugs with Clang's consumed an... by jcranmer on Wednesday July 10, 2019 @03:15PM (#20405394)
My point is that doing nothing causes copy semantics to be generated by default. It requires some action (specifying move constructors, for example) to override the silent generation of copy semantics. In other words, if you're not explicitly thinking about whether copy semantics are appropriate (or even legal!), then C++ decides for you that they are. That's not a good default.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by namirez on Wednesday July 10, 2019 @04:48PM (#20406377)
Again not true! If you do nothing, the compiler generates both copy constructors and move constructors. If you pass an xvalue reference, the move is used. If you pass an lvalue reference or a value, the copy is called. The compiler doesn't do anything silently.
Re:Catching use-after-move C++ bugs with Clang's consumed an... by jcranmer on Wednesday July 10, 2019 @05:18PM (#20406665)
Again, you're not understanding, or perhaps, you're assuming that users have a much more thorough understanding of the precise semantics of C++ than they usually do.

It generally takes extra effort to force the compiler to use a reference or the move constructor instead of the copy constructor. For example, "for (auto x : container)" will usually involve a copy constructor where a reference might have sufficed. And because copy constructors are generated (unless you take extra effort), there can often be no indication that the most natural form of the construction is actually less efficient than originally desired.

Yes, if you're aware of the gotchas, you can avoid them, but it is consistently extra effort that has to be put into doing so, and slipping up and missing the efforts in a few key places means you won't even be alerted that you might have forgotten something.

Re:Catching use-after-move C++ bugs with Clang's consumed an... by namirez on Wednesday July 10, 2019 @05:23PM (#20406716)
I understand your point, but the language is evolving to be more powerful and expressive. There were certain rules in the past, and there are some new rules in C++11 and beyond.

With regard to your example, that's why you need to use universal references like "for (auto&& x : container)" [1]. It's just a new way of doing things, but it's hardly ambiguous.

[1] https://isocpp.org/blog/2012/11/universal-references-in-c11-...