4 ms·
Most explanations of C++'s std::move fail because they don't focus on its actual effect: controlling function overloading. Most developers have no trouble gett
by quietbritishjim 2y ago
Most explanations of C++'s std::move fail because they don't focus on its actual effect: controlling function overloading.
Most developers have no trouble getting the idea of C++'s function overloading for parameter types that are totally different, e.g. it's clear what foo("xyz") will call if you have:
void foo(int x);
void foo(std::string x);
It's also not too hard to get the idea with const and mutable references:
void foo(std::string& x);
void foo(const std::string& x);
Rvalue references allow another possibility:
void foo(std::string&& x);
void foo(const std::string& x);
(Technically it's also possible to overload with rvalue and non-const regular references, or even all three, but this is rarely done in practice).
In this pairing, the first option would be chosen for a temporary object (e.g. foo(std::string("xyz")) or just foo("xyz")), while the second would be chosen if passing in a named variable (std::string x; foo(x)). In practice, the reason you bother to do this is so the the first overload can pilfer memory resources from its argument (whereas, presumably, the second will need to do a copy).
The point of std::move() is to choose the first overload. This has the consequence that its argument will probably end up being modified (by foo()) even though std::move() itself does not contain any substantial code.
All of the above applies to constructors, since they are functions and they can also be overloaded. Therefore, the following function is very similar in most practical situations since std::string has overloaded copy and move constructors:
void foo(std::string x);
- ajross 2y ago[flagged]
- quietbritishjim 2y ago> This language feature is best understood as an attempt to mitigate the complexity of this other language feature That doesn't really make any sense. Move semantics aren't meant to mitigate complexity of function overloading. It's more like, it uses function overloading as part of its implementation. I do strongly prefer Rust's strategy of move being the first class citizen (and being destructive), with copy (/clone) layered over the top. But of course C++ got where it is for historical reasons. And I've never used Rust properly so I don't really know if the grass is greener. > Rust isn't as far back on that road is it pretends and is likely catching up. This is just vacuous trolling.
- ajross 2y ago[flagged]
- gpderetta 2y agoC++ is definitely complex, probably too complex, but "This language feature is best understood as an attempt to mitigate the complexity of this other language feature" is, aside from being wrong, needlessly inflammatory.
- sa1 2y agoCould you go into some details into how it's wrong and how you interpret this statement? Prima facie, it does seem correct to me.
- gpderetta 2y agoI'm not even sure what feature std::move is supposed to be mitigating. Certainly not overloading, in any meaningful sense.
- johnnyanmac 2y agoPrima facie: what does std::move look like it's trying to band-aid fix? copy operations? They expanded the Rule of 3 to the Rule of 5 for a reason. The operations work together. Move semantics simply work as an optimization when you know the data you're taking in won't be needed anymore and you can more or less "snatch" the data (to put it overly broadly). If you can't do that, you take a slow path and just copy properties one by one. Garbage collection? Well, besides these languages not having that, managed languages are making the exact same decisions under the hood when optimizing. if it knows a reference is being assigned but then goes out of scope, it can do similar move semantics to save on a copy.
- ajross 2y agoSigh. That statement was literally a very straightforward paraphrase[1] of the upthread point, which I even quoted. That one seems "inflammatory" and the other doesn't speaks more to the reader's perspective than the text's. But by all means, let's not debate programming language complexity and call people vacuous trolls instead. [1] "explanations fail because they don't focus on" -> "best understood as". I mean, come on. I'm not reading much bile here. You added that.
- rocqua 2y agoTo clarify, you are saying the point of std::move is that it returns an rvalue reference, allowing the called function to pick the overload variant that is allowed to trample and destroy it's argument? Specifically, what you did not make clear is the return type of std::move.
- ryanianian 2y agostd::move is just a cast operation. A better name might be std::cast_as_rvalue to force the overload that allows it to forward to move constructors/etc that intentionally "destroy" the argument (leave it in a moved-from state).
- tialaramex 2y agoThey don't destroy the argument - this is of course a big problem because the semantic programmers actually wanted (even when C++ 98 didn't have move and papers were proposing this new feature) was what C++ programmers now call "destructive move" ie the move Rust has. This is sometimes now portrayed as some sort of modern idea, but it actually was clearly what everybody wanted 15-20 years ago, it's just that C++ didn't deliver that. What they go was this awful compromise, it's not destroyed, C++ promises that it will only finally be destroyed when the scope ends, and always then, so instead some "hollowed out" state is created which is some state (usually unspecified but predictable) in which it is safe to destroy it. Creating the "hollowed out" new state for the moved-from object so that it can later be destroyed is not zero work, it's usually trivial, but given that we're not gaining any benefit by doing this work it's pure waste. This constitutes one of several unavoidable performance leaks in modern C++. They're not huge, but they're a problem when you still have people who mistake C++ for a performance language rather than a language like COBOL focused intently on compatibility with piles of archaic legacy code.
- Maxatar 2y agoThanks for pointing this out. It's an absolute myth that C++ move semantics are due to backwards compatibility. The original paper on move semantics dating back 2002 explicitly mentions destructive move semantics by name: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1377.htm https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n13... It does bring up an issue involving how to handle destructive moves in a class hierarchy, and while that's an issue, it's a local issue that would need careful consideration only in a few corner cases as opposed to the move semantics we have today which sprinkle the potential for misuse all over the codebase.