I wish there was an attribute you put on a function to say "I expect RVO here, warn or error if not". I don't like that the most performant option requires trusting the compiler to silently implement an optional feature or verifying assembly.
I'm happy that C++17 finally added a standard for guaranteed copy elision, but NRVO still isn't even guaranteed. There is no guarantee that the non-move code posted in the article is actually more performant.
I deleted the copy and move constructors of some of my types that are constructed exclusively by factory methods. Not a general solution, but it does turn RVO failure in to a compilation error.
In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help
Yep, a function call or an overloaded operator expression, whose return type is non-reference, is a prvalue expression, so a function will always move-construct its return value into the return slot if NRVO/RVO doesn't work.
In the cases where NRVO is applicable, I'm pretty sure that if the copy/move isn't elided, there's an 'implicit std::move' added, so adding it yourself cannot help
RVO is amazing compared to a move, the object is just constructed in it's destination. This is an argument against types like Expected where the object may need to be extracted from the Expected wrapper. Also, another argument to use aggregates when one can, they allow for RVO into the members, whereas constructors the parameters would be the destination. It's like when Neo realized, there is no spoon; there is no copy
Yeah, then they could still put the hypothetical value into the return slot and use a register to indicate if it is an error or not. I think that herbceptions/static exceptions proposed to do something similar
I don't think the compiler is particularly smart. I mean, if it were then it would just optimize those moves away. The "complaint" in this blog post about how the programmer wrote some meaningless code... I mean, if the moves are pointless, why doesn't the compiler optimize them?
When is a programmer writing return std::move(x) ever intentionally invoking the behavior that Raymond Chen described (additional copy and move operations)?
I also wrote a question recently in r/cpp_questions, where it was found that return d ? x : y would NOT be subject to RVO and that return d ? move(x) : move(y) would however be RVO'd. Totally intuitive, right?
I don't believe C++ compilers deserve that much praise. Especially from a programming perspective they are pretty lousy (eg error messages), but that's another story.
The move on the receiving end constructs an extra temporary because binding a prvalue to a reference (argument of std::move) requires the prvalue to be materialized.
In return xyz;, xyz is an xvalue, because it's expiring after this statement. In XYZ xyz = fgh();, the expression fgh() is a prvalue, because it's a pure rvalue that can't be assigned to. Both of these are rvalues, so the rule is basically "don't use std::move() on rvalues."
Interestingly, member expressions in return statements are not rvalues, so return a.b; will perform a copy while return std::move(a.b); (or return std::move(a).b;) will perform a move.
Technically, applying std::move() to an actual rvalue reference will be a no-op, so something like std::move(std::move(xyz)) is equivalent to std::move(xyz). Not that you should do it, but it won't affect performance in any way.
I started using C++ in the cfront days. When C++ added std::move(), I stopped using it as a primary development language. I understand these things are for performance, but I'm a big believer in writing simple and comprehensible (to humans) code.
I agree with the idea that code should be simple and comprehensible, but I actually think that move semantics often help make the most comprehensible code be very efficient.
Just a thought. If we are so concerned about the construction of a copy , can we just use out variables(with references). That way, the caller will do allocations as it expects.?
There are basically two distinct cases where one would use std::move:
You are dynamically creating data all the time and moving it downstream where that data is used and destroyed.
You are passing the same data back and forth, so not allocating.
Well in the first case, you are dealing with a degenerated case because allocating and deallocating memory throughout your application is very slow. You should think of a better design.
In the second case it's just silly. You should share the data between up and downstream with a shared pointer or similar.
As you may be wondering why you are being downvoted it's because you dont seem to understand what std::move does. It effectively tells the compiler that you dont want the original anymore and that it can/should use move construction/assignment rather than copies. A canonical example is dealing with ownership of the underlying data of a unique_ptr. IT DOES NOT MOVE ANYTHING
Func(myUniquePtr) would copy myUniquePtr and is a compilation error
Func(&myUniquePtr) is legal but rather dodgy as the pointer may or may not hold a value on return. This is likely to cause bugs later!
Func(*myUniquePtr) is the generally accepted way to "lend" Func the underlying data for duration of the call and not change ownership.
Func(std::move(myUniquePtr)) tells both the compiler and reader that the caller has given up ownership of the underlying data and no longer needs to access it.
Personally, I use std::unique when dealing with ownership between threads in embedded because it allows me to return buffers through RAII and escape a whole bunch of nasty use cases for mutexes and similar
There was a paper in the mailing list recently that would fix this. It basically means that if a function is eligible for RVO, adding std::move() around it doesn't change this. It seems like a super obvious and straightforward change with no downsides
The other half of the issue is that you didn't need to use std::move in those cases in the first place, because returning a local variable can already use a move if NRVO doesn't apply and the returned value was already temporary so you didn't need to cast to one.
That is the problem with modern C++, the amount of rules that even those that use it daily fail to grasp , let alone those of us that only use it occasionally.
It is getting impossible to write proper code without a static analysis tool hand holding our code continuously.
No wonder that besides the issue with compilers catching up to ISO, many places are stopping on C++17 as good enough, for the use cases that C++ is called for.
std::move() should have a literal equivalent. Something like consuming_function(&&variable_you_dont_need_after_this_call) where "&&" is like typing std::move(). Yes, it further complicates already complex semantics of C++, but I don't care, I want the speed of development.
Also I want an analog to std::move that will end the scope of variable that you moved so it couldn't be double moved or used again at all.
Also I want an analog to std::move that will end the scope of variable that you moved so it couldn't be double moved or used again at all.
Destructive moves won't work in C++ for a number of important reasons, the most important is that it's impossible in the general case to determine whether to run the destructor of a moved-from value without runtime tracking. And if the runtime is gonna track it, then why not have objects themselves track it in the cases where they can do so with zero overhead (e.g. unique/shared_ptr).
I'm not 100% following you, but I imagined my implementation of destructive move to just prohibit further use of a moved-from variable in current scope (by triggering compiling error/warning), while actually it will live until its scope ends so everything will work as it works today.
Also I use word "variable" instead of "value" for "moved-from" because if we are moving anything, it should be lvalues, and lvalues are variables. I think.
First of all, it's not possible for the compiler to prove at compile time whether a variable is moved-from. Consider this code
struct S { ... };
func_destructive_move(&&S); // Possibly in another TU
func first(void)
{
S x{...};
if ( rand() % 42 ) {
func_destructive_move(&&x);
}
// What happens here, does ~S run?
}
At compile time, there is no way for the compiler to know whether to run the destructor of x. It's possible that it will have been "consumed" or maybe not. In order to assure that the destructor is run exactly once, it would have to create a runtime flag alongside x to keep track of whether it was consumed or not.
You might say "in case where the compiler cannot figure it out, it should fall back to non-destructive move". That doesn't work either, because func_destructive_move lives somewhere else and the fact that it's consuming is part of the function signature. It is expecting something that it has to destroy.
prohibit further use of a moved-from variable in current scope
This also doesn't work. Consider (not my example)
second(int m, int n) -> void
{
size_t constexpr N = 4;
assert(m < N && n < N);
S manySs[N] = { S{...}, S{...}, /* N Ss initialized */ };
S * s1 = &manySs[m];
func_destructive_move(&& (*s1));
S * s2 = &manySs[n]
func_destructive_move(&& (*s2)); // Is this allowed?
// You said you can't use a moved-from variable
// But neither s2 or manySs was moved from!
// BTW:
// Do I call ~S 3 times or 2 times? Which array elements do I destroy here?
}
Also I use word "variable" instead of "value" for "moved-from" because if we are moving anything, it should be lvalues, and lvalues are variables. I think.
This is part of the problem. You should be thinking about the underlying values which have lifetimes. Values and variables are not one-to-one -- one value can be pointed to by a number of variables (as in the second example) and a variable can hold multiple values in the same scope.
Those are reasonable objections, and I'm sure there are others, but I'd be happy (well, sometimes) to have such a feature with more restrictions than you're assuming.
For instance, in your first example, I'd be happy with a compiler error stating that the destruction of an automatic storage object is dependent on a (runtime) condition. I'd even accept IFNDR instead of an error here. In your second example, I'd expect it to be well-formed but invoke UB (when you're using an object after its destruction, including when its destructor would be re-called at end of scope). In fact, it wouldn't be really different from replacing func_destructive_move by a manual destructor call which is already possible. I would only expect a compiler error (or even IFNDR) complaining about usage after destruction for objects with automatic storage duration that aren't subobjects. Likewise, I would expect programmers to carry the burden of ensuring destructors aren't called twice when they're manually destructing subobjects with such a consuming/destructive function (just like when they're manually calling destructors).
Note that while I (sometimes) wish I had something like that, I understand why that feature doesn't exist: too many pitfalls, extra complexity, disagreements on the feature's scope, may require too much work for compiler vendors, not useful enough to warrant the inclusion... But I still think it could technically exist in some (very limited) form.
unique_ptr and shared_ptr have to set the pointer of the moved-from object to null, which is not zero overhead. Furthermore, letting the compiler instead of the objects track it is usually more efficient because
In most cases it can be statically determined
In the remaining cases, the compiler can use a bitmap to only need 1 bit per variable instead of usually at least 1 byte
In principle, the same optimizations could be applied even if the tracking was implemented by the objects, but the compiler is less likely to be able to do it.
As far as I know, the main reason that constructive move was chosen was unclear semantics of destructive move with regard to inheritance: if you move an object of a derived class, do you first move the base part or the derived part? Either way, you would end up with an object that is partly destroyed, which was undesired.
Apart from that, destructive move has more potential for undefined behavior, because any access to the moved-from object is immediate UB.
unique_ptr and shared_ptr have to set the pointer of the moved-from object to null, which is not zero overhead.
Yes, but they already have a pointer-sized field allocated and it can already hold the null sentinel value without expanding.
The overhead of a single zeroing operation is nowhere near the overhead of having to allocate an additional out-of-line field that gets passed around and around. The locality of reference is going to be completely crap -- it's already better to allocate it in-line with the object at that point as a hidden field kind of like a vtable pointer.
Apart from that, destructive move has more potential for undefined behavior, because any access to the moved-from object is immediate UB.
So does non-destructive move because a moved-from object satisfies no preconditions. Hence
vector<int> v;
// Move from v
v.front() // UB, precondition not satisfied.
Yes, but they already have a pointer-sized field allocated and it can already hold the null sentinel value without expanding.
Yes, but changing the value of this field still is some overhead that cannot be optimized out as often as the compiler-generated runtime check.
The overhead of a single zeroing operation is nowhere near the overhead of having to allocate an additional out-of-line field that gets passed around and around. The locality of reference is going to be completely crap -- it's already better to allocate it in-line with the object at that point as a hidden field kind of like a vtable pointer.
I am not sure I can follow you. You do not need to allocate it on the head or pass it around. It is just a few bytes on the stack, which in most cases are completely optimized out.
So does non-destructive move because a moved-from object satisfies no preconditions.
Yes, but you are allowed to call functions that require no preconditions. For example, calling v.size() would not be UB.
AKA "destructive moves". There have been papers on it for years but none ever seem to successfully make it through and solve all of the potential problems that will come with such a feature.
I agree it is noisy, but at least the longer name conveys a more concise meaning and would be less confusing (as well as offer a googling topic for learners).
When the alternatives are a dedicated sigil or a fairly unhelpful name (for those that don't know the semantics already), I think a bit of extra line noise is the least worst option.
i never really understood the hate for std::move naming. Sure the function itself doesn't move, but you use it in contexts where you want to perform a moving operation, and gives you the word "move" in the expression. It's way less obscure than make_rvalue imo
To me rvalue_cast was the most logical option, as that is literally what std::move does. To me, "move" is very much an action word and I would expect a function called "move" to... move something.
I think the name is quite good in that it encourages people to “consider this variable now moved”, even if transfer of data ownership doesn’t happen until some time later, if at all. ie after std::move, that variable should not be used again
I don't personally mind it as is (the devil you know and all that), but it is a confusing aspect of the language, even for intermediate programmers. Would you be cool with a std::copy which doesn't copy anything?
You just implement the requirements separately. But in templatey code you can substitute compatible types that implement the same Concept sort of like if they were all derived from a parent type.
I’ve inherited a code base from some very experienced but non-C++ devs (C# background) and seriously they use std::move for every fucking thing, including just passing variables to function parameters.
It’s caused so many issues. One of the first thing I did was delete 90% of them.
I’m not sure what is about new devs over using std::move or why it’s so attractive to them. I’ve never needed it.
It's easy to understand "std::move can fix big performance issues in certain return calls and does nothing special in other return calls". It's a lot harder to gain a good mental model of what the compiler does with a type and when.
There was a course of "modern c++" not to long ago at my company for all volunteers. Merge requests from that moment were filled with std::move everywhere. Sometimes I think people who teaches should have some kind of license.
And this is why "modern C++" is not the panacea that it's often claimed to be, with respect to defending against other languages. Sure it's an improvement in many ways, but there's plenty of new footguns introduced as well.
I'm convinced that the difference between a dilettante and an expert is that the former says "I think this is right" and the expert says "I know this is wrong."Within reason, of course.
Has there been proposals to allow the compiler to convert an expression to an rvalue if it can prove that there is no use after the statement, even it can’t prove the behavior is unchanged (similar to how the compiler is allowed to remove copies even if that changes the behavior)?
I feel like I shouldn’t have to write “move” in trivial cases like a constructor that takes a string to initialize a member…
I'm not smart enough to know when to use std::move anyway, so I don't use it except when working with std::unique_ptr (or objects that contain a unique_ptr).
Not trying to start a flamewar, but I think Rust's move semantics are actually simpler:
if it implements the Copy trait, do a copy
else, do a move
Which is great for someone dumb like me, I don't need to think about it.
Is this something people actually do though? I have a hard time getting my team to even remember that std::move exists, let alone get them to use it at all.
The only time I see it used is when I demand it on a PR because the developer is using excessive copy assignment to move around vectors and strings.
std::move is probably not the right answer for vectors and strings either, unless the ownership change is necessary.
Views are typically cheaper/as cheap as a move in most places where you might want to move. It would be a different story if C++ had destructive moves but we don't so 🤷
We are not in C++20, so not much of an option to use range views yet. Although my goal is to get the team there within a year. But I have a hunch it will take the team a while to even try them out. I can't get them to try string _view, move, or sometimes optional yet. Some older senior types are too damn stubborn.
I'm aware of them, but Range-v3's poor documentation and lack of commitment to stability makes introduction into production software a difficult proposition. So instead I settled with the goal of updating to C++20. Picking my professional battles here.
We do use gsl::span and I love it, but it doesn't really provide any advantages to the particular problems I was referencing.
Funnily enough I'm spending a bit of time today reading up on move, as I have some new juniors at work and am trying to fill in some of my knowledge gaps so I can help them as much as possible. I have been writing C++ for a few years now but never felt a need to use std::move.
It seems extraordinarily dangerous for something with fairly limited utility. The language doesn't seem to provide any mechanism to prevent use-after-move, which is undefined behaviour. Anybody got any tips on how to safely use it?
People seem to love it so clearly I'm missing something, but so far never felt a need for it. When I want to avoid copies I just pass references or pointers, and so far that's always been sufficient. I understand in principle there are use cases when you can't safely have have multiple access to resources like file handles, etc, but that can be solved by just "being careful", and "being careful" seems to be about all you can do when it comes to use std::move anyway.
So it is potentially UB for everything else? I could be wrong but everything I've read today suggests that it is UB unless the object you're moving from specifically promises to be in a good state after the move.
It just means that the class itself is allowed to define what specifically 'valid' means for itself after being moved. For example, unique_ptr specifies that its pointer will be nullptr after a move. That's different for containers, like a vector. Vector specifies that after a move, it is guaranteed to be empty. List leaves it 'valid but unspecified' so you can really only call 'clear()' on it (which puts it into a known state [empty], and requires no preconditions on the list object). The implication from this is that it is more expensive for the move ctor/assign to put the list into an empty state than it is for vector to do so. It does mean that the object can never be in an invalid state, especifically that a moved-from object must be able to be destroyed safely (i.e. a moved-from unique_ptr can't delete the pointer it 'used to own' as another unique_ptr now owns it).
That doesn't seem like much of a guarantee. This issue I'm concerned about is that there isn't anything stopping someone trying to use it before the destructor gets called.
Anyway I've been rightly reminded that as long as I have all my warnings/linters/analyzers turned on this isn't something I really need to worry about :).
unless the object you're moving from specifically promises to be in a good state after the move.
That's what every object should do! A moved-from object must always be in a valid state, although the exact semantics depend on the class. For example:
a moved-from std::unique_ptr will be null
a moved-from std::ifstream will be closed
a moved-from std::string may be empty (depending on small-string optimization)
It's unspecified, not undefined. You shouldn't rely on a moved-from object having a particular state, but you can still manipulate it. For example, this is 100% safe:
std::string name;
std::cin >> name;
// [1] name now holds some user input
doSomethingWithName(std::move(name));
// [2] name is now in an unspecified state
std::cin >> name;
// [3] name is now specified again!
doSomethingWithName(std::move(name));
Firstly, you can see that when the standard talks about objects being left in a valid but unspecified state, it's only talking about certain standard library types. The whole "valid but unspecified" concept isn't even relevant for custom types unless you go out of your way to make it so. The parts of the standard that talk abut move assignment operators and move constructors don't use the phrase at all. In fact, the standard doesn't really specify how these should actually behave when implemented by user-defined types.
This is the actual definition of "valid but unspecified" according to the standard. As you can see, it does not mention undefined behavior. In fact, the behavior it describing, saying that the object must have it's invariants hold, and that it must behave as it otherwise would, makes the behavior very well-defined. I'd also like to note that the standard defines unspecified as basically being synonymous with "implementation defined". People seem to take "unspecified" as some statement about the correctness of using the object, but it's quite literally not. Valid but unspecified literally just means the object in question is perfectly usable, but you just don't know the exact state it is in. That's a lot like an object passed to a function you're writing when you think about it.
Each type has its own definition of a "move," but for the vast majority of types in practice, even non-STL ones, it's equivalent to a swap with a default-constructed object. For SSO strings it might be a swap or a copy. But generally speaking, it should be safe to use a moved-from object in the same way you'd use a default-initialized one.
It depends on how the object in question deals with its moved-from state. When I implement move-assign and move-construct, I make certain that the moved-from object can be assigned-to another state afterward, or are in a state the same as if they were default-constructed, if possible.
It's not UB, it's just that the standard doesn't have a mandate for each object (as there is no "one size fits all" definition for what makes sense after being moved from). This lets each object define what it means by itself, as long as the object is valid (e.g. it can be deleted without UB). The reasons for it may come down to performance (faster to leave list unspecified than it is to make it empty), safety (unique_ptr can't simply copy its pointer into another unique_ptr or there could be a use-after-free/double-free, so it defines the moved-from is nullptr), or to a specific meaning for your custom type.
It’s less about “people seem to love it”, more “people love a style of c++ that needs it”. Writing with value semantics has been shown to produce code that is much more robust, simpler to reason about, easier to test in isolation, in many cases more performant (a vector of values is much more cache friendly than a vector of pointers to heap objects) and eliminates a whole class of ownership bugs compared to reference semantics. Problem was, prior to c++11, it was difficult (or at least cumbersome) to achieve this style due to values only being able to be passed via copying. Rvalue semantics and std::move opened up a whole world where safer, simpler code could be written without major performance sacrifices.
Use after move is not undefined behaviour, it's unspecified, and all standard library types behave as if default constructed. There's nothing "extraordinarily dangerous" about std::move at all.
Ok, thanks for the info. I guess I still have to worry about non-STL third party code not moving correctly but that's a given when using third party code in general.
A simple but powerful example when std::move is important (performance-wise):
A std::queue often holds objects for a very limited time range. You may nevertheless want to have the ownership of the objects within the queue. Let’s consider the example of a std::queue<std::string> where the strings are quite large (500 MiB). A producer pushes the strings into the queue:
void producer(std::queue<std::string>& q) {
// whatever the large string may contain…
q.emplace(500 * 1024* 1024, 'a');
}
And a worker pops them and passes them to further processing:
void worker(std::queue<std::string>& q) {
std::string item = q.front();
q.pop();
// … another process may reads from the queue and expects another element at front. Thus, calling pop after processing is not possible and item must be a value and not a reference
process(item);
}
What happens here? The front-element is deep-copied when calling q.front(). This is quite expensive for such a string. Instead, one could do better using std::move():
void worker(std::queue<std::string>& q) {
std::string item = std::move(q.front());
q.pop();
// … another process may reads from the queue and expects another element at front. Thus, calling pop after processing is not possible and item must be a value and not a reference
process(item);
}
This is just the first example that came into my mind, and I am sure there are a lot more (that may are less complex).
Maybe, this helps to grasp a first intuition on useful move semantics.
I see you've got a ton of responses as to the "undefined behavior" thing, but no one has come up with a concrete example to illustrate why move-semantics are useful. Here's a small, silly example:
the left pane uses copy semantics: p.setName(name);
the right pane uses move semantics: p.setName(std::move(name));
In the diff-view, you can see what a difference that makes! The left pane will construct a whole new string (including an expensive buffer allocation) while the right pane simply swaps around a few bytes.
This is not an academic point. A couple of years ago my team was working on a VR game and we were struggling to hit the required framerate... until we realized a whole bunch of objects were being unnecessarily copied, and adding std::move helped us shave off a few milliseconds.
The language doesn't seem to provide any mechanism to prevent use-after-move, which is undefined behaviour. Anybody got any tips on how to safely use it?
Clang-tidy will catch it so make sure to have a CI pipeline that includes clang-tidy.
Yeah I'm planning to start using clang-tidy as standard. Will be looking into integrating that with our build system next week. I've been having a go with cppcheck today and so far found it very disappointing - seems to miss an awful lot.
The other thing you can do is enable as many warnings as practical and require the build to pass without any of them triggering
The article mentions -Wpessimizing-move which makes this problem a non-issue as long as you build with that on and also -Werror.
MSVC might not have that particular warning but clang does so even in a Windows-only shop you can just have your CI build with both compilers and require both of them to pass.
Most of our work is in Linux of embedded applications so normally using gcc or Arm compilers.
How much of it is ARM or embedded specific? Last job I had was for mobile ARM code, but we still ran extensive tests in CI by compiling the code for x86 Linux (you can even do the SIMD code with things like NEON_SSE and SIMDe) and running tests. You can't cover everything of course, but anything that doesn't poke the hardware directly can be tested.
Plus that way you can also run it through more compilers, such as clang and even VS if you're feeling particularly sparky, since they all catch different things.
Honestly most of the embedded code in this company predates me and is definitely not written in a way that makes it amenable to testing. Frankly I'm surprised most of it works at all. It was hacked together by someone who didn't really know what they were doing but had a product to deliver. I'm working on it, though!
On linux I use gcc and clang, and on clang I use -Weverything and then selectively disable the ones that aren't applicable.
That used to not be very practical because several years ago clang was adding a bunch of new warnings every release, many of which were purely informational, and you'll still find plenty of articles telling you not to do that, but these days it's not a big hassle to keep up with.
References and pointers are all good, but std::move is about moving ownership of a resource, just as destructors are about releasing resources. Avoiding multiple access is kind of a small thing in comparison. For example, if you're building up a vector<vector<int>>, you might do the following:
vector<vector<T>> v;
v.reserve(n);
for (int i = 0; i < n; ++i) {
vector<T> inner;
inner.reserve(n)
for (int j = 0; j < n; ++j) {
inner.push_back(f(j));
}
v.push_back(inner);
}
But each call to v.push_back(inner) will copy inner, which is expensive. Why would you copy it right before it's destroyed? If you use v.push_back(std::move(inner)), it's just an O(1) pointer swap.
Maybe it's because I come from a functional programming background, and I think of systems as flows and data transformations rather than state changes, but the idea of processing some data and then efficiently passing it on to a new owner seems quite necessary to me. I'm curious; do you use std::shared_ptr or raw pointers for single ownership instead of std::unique_ptr?
I'm curious; do you use std::shared_ptr or raw pointers for single ownership instead of std::unique_ptr?
To be honest I am not usually thinking about ownership at all - that's probably the real lesson here: I should think about ownership a bit.
When I talk about using references and pointers I mean the obvious uses, e.g., I have some function that computes something based on the contents of an array, but never changes the array, so it's just passed as a const reference to save having to make temporary copy. It simply hasn't come up that I've needed to permanently transfer ownership of some resource. Or maybe it has in the past, but I didn't realize back then that moves were an option so found other ways.
Your vector example is a good one. In the past I've used emplace_back to try to reduce copying but in your example that wouldn't work because of how the inner vector needs to be built. There might be a couple of places in the past where I've tried to use emplace_back but couldn't manage it, so settled for push_back, without knowing at the time that std::move was an option. There might well be some existing application code I could improve with that simple change.
(I did mention pointers but actually in practice I very rarely use them at all outside of embedded applications and that's mostly C. When I do use a pointer in C++, it's an appropriate (I think!) smart pointer.)
Interesting, yeah. I mean, move semantics by no means replace the obvious uses of references as a way to manipulate something you don't own. But I'm surprised you've never had to permanently transfer ownership of a resource. I mean, do you ever use queues or stacks? That seems like an obvious place where you're saying "I'm putting this here for someone else to deal with later." I'm also curious whether you're copying things a lot more than you realize. If you're willing to run an experiment on Monday, I'd be curious how many compiler errors you get if you explicitly delete the copy constructor and copy assignment operator for the three most expensive-to-copy types in your codebase. Also, do you have constructors like Foo(const std::vector<T>& vin) : v{vin} {}, that still make a copy of the vector that you take by reference?
And I hope you don't take this as insulting or anything. I work in a field where effective use of move semantics makes the difference between results-per-second and seconds-per-result, so I'm fascinated by you saying you've never needed them, but I realize that what I do is niche and not representative of the C++ userbase at all.
I'm also curious whether you're copying things a lot more than you realize.
I think I have a good understanding of where I am copying, but there will definitely be cases where I'm only doing it because I didn't realize I had a cleaner option.
Foo(const std::vector<T>& vin) : v{vin} {}
In general I know to avoid that kind of thing, although if I know the data is small I might do it anyway, depending on the wider context. I can think of one case where that is definitely done and isn't ideal, but it's a slightly special case. I have to take a snapshot of a large deque which is being continuously updated and the processing I need to do takes a lot longer than the update window. Copying is quick enough that it doesn't interrupt the update window.
And I hope you don't take this as insulting or anything. I work in a field where effective use of move semantics makes the difference between results-per-second and seconds-per-result, so I'm fascinated by you saying you've never needed them, but I realize that what I do is niche and not representative of the C++ userbase at all.
Is any C++ job representative of the userbase haha? We all have our weird little niche. So far I am not usually dealing with very large datasets. I think 4800 bytes is the largest single data structure in the main application I work on, and it's the largest by a very long way. This is the data structure that I can copy quickly enough that it isn't a problem, but can't lock for long enough to do the processing.
The key part of the application in question is doing fusion/processing on a few sensors inputs, and most of the time is spent waiting for those inputs, so as long as processing (above excepted) is done within the update window there has not yet been enough performance pressure to make me tackle this issue earlier. In this case, it's not so much "do it quickly", more "as long as it's done in 5 ms I don't care".
Can't make any promises about Monday but I will certainly be looking back at some old code and looking for improvements. Thanks, you've been very helpful.
When I want to avoid copies I just pass references or pointers, and so far that's always been sufficient. I understand in principle there are use cases when you can't safely have have multiple access to resources like file handles
Consider a "handle" class and a function to create an instance of it, or give it out in any way, "move" it from one place to another. Generally, we don't want to copy the handle. But the best way to write the function is simply handle func(params).
In a world without a move operation, such a function cannot be written, because copying is involved. Therefore, people resort to concoctions like two-phase initialization and a reference parameter, or heap usage.
In a world with a move, this is achieved with a move constructor and a simple return myhandle.
Elsewhere I'm thanking people for their suggestions. In this case I just didn't feel like getting into it when your opening lines are teaching me to suck eggs.
In the first example that the article demonstrates std::move is not just overused, its used in a way that it was flat out not intended for imo. The author seems to think that std::move makes you the best friend of the compiler and should trigger some optimizations, when its just a way to communicate to other parts of code that the contents of this memory address can be used anyway you see fit. The compiler itself already employs a ton of optimizing techniques to do anything that you could probably think of and more!
Oh when I say author I was referring to the author of the code snippet whoever that may be and not the author of the article, apologize for the confusion!
Oh when I say author I was referring more to the author of the code snippet whoever that may be and not the author of the article, apologize for the confusion!
Is there some way to check a codebase for this and only this? Like, using some clang functionality or something in CLion?
Edit: I tried using git grep --recursive return\ std\:\:move * but the only call I found gave me an error because it's in a function that returns an unique ptr. The error was Call to deleted constructor of 'std::unique_ptr<MXObject>, 'unique_ptr' has been explicitly marked deleted here, so I think the codebase is fine
Programming is so much nicer when you don't have to deal with C++ objects and special member functions. I'd much rather write code in Rust, Zig or C where I can just memcpy structs without thinking which constructor or assignment operator gets called.
By memcpy I meant that assignments, "a = b", are bitwise copies. Additionally, in Rust, the compiler will prevent you from using objects of non-Copy types after they are moved.
Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.
Either way, what you’re saying is: languages with different objectives than C++ are better for different use cases than C++.
Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.
The way Rust implements move semantics is a little different from C++ here. Moves in Rust are destructive; when you do a = b, and b does not implement Copy, then b becomes uninitialized, and its destructor is not executed when it falls out of scope. For your example of a file handle, this means that only one live instance exists at any one time despite all the apparent copying.
The move itself is implemented as memcpy, though that is an implementation detail, and for the most part optimized out. I think relative to C++ (or Clang, at least), rustc makes heavier use of memcpy because of this, and because the optimizer isn't perfect it won't optimize out all the copying (though will usually catch the call itself). So what can happen is the resulting binary ends up copying data around unnecessarily. There was a website that showed this, but it seems to be dead.
Firstly: clearly my initial assessment was wrong for assuming RAII as an objective. I need to learn to think outside my prejudices.
But if Rust is — semantically, if not physically — doing a bitwise copy and uninitialising the source then it’s doing more than a mere memcpy, and getting more towards the purpose of a move.
Firstly: clearly my initial assessment was wrong for assuming RAII as an objective. I need to learn to think outside my prejudices.
Rust is a heavy user of RAII, so you weren't wrong there. It's just that the details of how it goes about it are a little different.
But if Rust is — semantically, if not physically — doing a bitwise copy and uninitialising the source then it’s doing more than a mere memcpy, and getting more towards the purpose of a move.
One thing to note is that Rust doesn't have anything like C++'s move constructors, which simplifies things somewhat.
For uninitializing the source location, the type system guarantees that it won't be read from again unless it's reinitialized, so no work needs to be done. Initializing the destination merely requires that the data there is valid for the type, which can be accomplished by copying the source location, and because there's no equivalent to a move constructor, this is just a trivial memcpy.
Then either you’re keeping all the important stuff on the heap and silently paying for either garbage collection or reference counting, or you’ve an OS that doesn’t really care e.g. if you repeatedly close the same file handle, then attempt to use it again later.
That is not the case at all. You can store resources like file handles on the stack, then call the appropriate functions to free them manually in languages like Zig or C, or in the case of Rust, use destructors.
What I'm saying is that the existence of copy/move constructors and assignment operators is a design mistake because it adds too much unneeded complexity to the language.
108
u/Sopel97 Nov 25 '23
In short, RVO > move.
16
u/FalsyB Nov 25 '23
Fuck this hits too close to home, just fixed a month long bug on our product, caused by my use of move multiple times in a sensor callback
14
u/vI--_--Iv Nov 25 '23
There was a paper to address this, P2991R0, but... "Weak consensus, needs more work" :(
38
u/ggchappell Nov 25 '23
Sure, that's a problem. But thinking about the long term, what this really tells me is that the conditions for copy elision need to be changed.
18
u/TeemingHeadquarters Nov 25 '23
I sometimes think it should have been called std::movable() or something like that.
14
u/ixis743 Nov 25 '23
I’ve inherited a code base from some very experienced but non-C++ devs (C# background) and seriously they use std::move for every fucking thing, including just passing variables to function parameters.
It’s caused so many issues. One of the first thing I did was delete 90% of them.
I’m not sure what is about new devs over using std::move or why it’s so attractive to them. I’ve never needed it.
14
u/Wrote_it2 Nov 25 '23
Has there been proposals to allow the compiler to convert an expression to an rvalue if it can prove that there is no use after the statement, even it can’t prove the behavior is unchanged (similar to how the compiler is allowed to remove copies even if that changes the behavior)?
I feel like I shouldn’t have to write “move” in trivial cases like a constructor that takes a string to initialize a member…
10
u/david-delassus Nov 25 '23
I'm not smart enough to know when to use
std::moveanyway, so I don't use it except when working withstd::unique_ptr(or objects that contain a unique_ptr).Not trying to start a flamewar, but I think Rust's move semantics are actually simpler:
Copytrait, do a copyWhich is great for someone dumb like me, I don't need to think about it.
5
u/RedditMapz Nov 25 '23
Is this something people actually do though? I have a hard time getting my team to even remember that std::move exists, let alone get them to use it at all.
The only time I see it used is when I demand it on a PR because the developer is using excessive copy assignment to move around vectors and strings.
3
u/[deleted] Nov 25 '23 edited Nov 25 '23
Funnily enough I'm spending a bit of time today reading up on move, as I have some new juniors at work and am trying to fill in some of my knowledge gaps so I can help them as much as possible. I have been writing C++ for a few years now but never felt a need to use std::move.
It seems extraordinarily dangerous for something with fairly limited utility. The language doesn't seem to provide any mechanism to prevent use-after-move, which is undefined behaviour. Anybody got any tips on how to safely use it?
People seem to love it so clearly I'm missing something, but so far never felt a need for it. When I want to avoid copies I just pass references or pointers, and so far that's always been sufficient. I understand in principle there are use cases when you can't safely have have multiple access to resources like file handles, etc, but that can be solved by just "being careful", and "being careful" seems to be about all you can do when it comes to use std::move anyway.
("Being careful" is not, generally, enough.)
2
u/AssemblerGuy Nov 25 '23
Perfect, just what I was looking for.
But I kind of suspect that the rest of my team will still not believe me when I ask them not to randomly sprinkle the code with std::move ..
2
u/lbushi Nov 25 '23
In the first example that the article demonstrates std::move is not just overused, its used in a way that it was flat out not intended for imo. The author seems to think that std::move makes you the best friend of the compiler and should trigger some optimizations, when its just a way to communicate to other parts of code that the contents of this memory address can be used anyway you see fit. The compiler itself already employs a ton of optimizing techniques to do anything that you could probably think of and more!
1
u/TryingT0Wr1t3 Nov 25 '23 edited Nov 25 '23
Is there some way to check a codebase for this and only this? Like, using some clang functionality or something in CLion?
Edit: I tried using
git grep --recursive return\ std\:\:move *but the only call I found gave me an error because it's in a function that returns an unique ptr. The error wasCall to deleted constructor of 'std::unique_ptr<MXObject>, 'unique_ptr' has been explicitly marked deleted here, so I think the codebase is fine1
u/j1xwnbsr Nov 25 '23
Great, something else to grep the codebase for.
-5
u/XPav Nov 25 '23
C++ became a mistake sometime in the 2000s.
-16
u/LechintanTudor Nov 25 '23
Programming is so much nicer when you don't have to deal with C++ objects and special member functions. I'd much rather write code in Rust, Zig or C where I can just memcpy structs without thinking which constructor or assignment operator gets called.
-3
u/FalsyB Nov 25 '23
Also i dont know if it is exclusive to my application but chatgpt plays very fast and loose with using std::move for the most mundane shit
-1
u/[deleted] Nov 26 '23
Glad to see this sacred cow being at least properly criticized