Earlier quoted context omitted.
> N/RVO works by (at the machine language level, of course) rewriting the function signature to return void and take an extra pointer parameter This sounds wrong, are you sure? Would you mind demonstrating with an example on godbolt? Whether NRVO applies or not, the ABI should be the same, AFAIK.
Yes, of that I'm sure. This optimization is only possible if the compiler has control of both sides of a call. If the function may be callable from other translation units or modules I imagine it generates a thin wrapper that's externally callable.
Move in C++ without a std:move
71–75 of 75 posts
Re: Move in C++ without a std:move
#72Re: Move in C++ without a std:move
#73If the author is reading: both complicated examples are the same.
Sorry for that mistake! I fixed the last example. The post should make much more sense now.
->
You probably don't pay for a copy or move there either.
(I'm Flemish and made that mistake before).
Re: Move in C++ without a std:move
#74Earlier quoted context omitted.
This is an easy mistake to make but it's not what happened Programmers already knew (~20 years ago) when the C++ move feature was designed that what people want is the destructive move assignment semantic, the thing Rust has today. Other languages did have that. But C++ 98 already existed and WG21 already did not want to make it difficult to take your crusty 10+ year old C++ codebase, slap a sticker on it and say thi…
> For example std::unique_ptr looks like it's Box but it's not, it's actually Option >, even newer types often do this but they might be more embarrassed about it. This would be the case even with destructive moves unless you also add some way for std::optional > to be no larger than a pointer by letting std::optional take advantage of the fact that a non-null std::unique_ptr has a bit representation that leaves room…
Re: Move in C++ without a std:move
#75Earlier quoted context omitted.
And the most important idea: destructive moves. Since C++ doesn't track lifetimes it has to leave the object in a "valid state" after a move and the destructor still runs which has to have a check if it should do something or not.
BTW, Rust's lifetime annotations for borrowed references are a mostly orthogonal feature. Liveness of objects for move/drop semantics is tracked differently, without any syntax and with implicit runtime drop flags where necessary. C++ could probably add the same deinitialized/moved-from state tracking (with an opt-in for back compat sake) purely to avoid dtor bloat, without having to add safety of borrow checking.