this post was submitted on 25 Nov 2023
27 points (96.6% liked)

C++

1767 readers
1 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 11 months ago* (last edited 11 months ago) (1 children)

To me it’s not really excitement so much as “oh god how do i make this ridiculous language do the correct thing”.

I think this all comes down to having the right mental model.

In this case, I think it helps to know that:

  • returning by value by default implies making a copy,
  • ...but if the output type supports move semantics and the function returns a temporary value (rvalue) then the language is on your side and tries to move it instead of doing a copy.
  • and here's the kicker: now the language goes the extra mile and looks after you to avoid that copy, and if your return value allows the language to apply an optimization then the language is on your side and eliminates any potential copy without you having to do anything special.

The zany behavior is that if you set up your function to push for a move (i.e., force a local variable to be treated as a temporary), the language ceases to be able to apply its optimization.

That's basically it. No real mystery.

[–] [email protected] 2 points 11 months ago

The parts that seem likely to cause this confusion (which I shared when I first started using C++11) are:

  • Moves in C++ are always a potentially-destructive operation on a reference, not just a memcopy.
  • Consequently, "moving" a temporary still requires having two separate instances of the type, despite that generally not being what you want, hence RVO.
  • ...but move-semantics are generally presented and understood as an "optimization", and conceptually "take the guts of this value and re-use them as a new value" is both what RVO is doing and what move-semantics are doing.
  • std::move isn't a compiler intrinsic and doesn't force a move operation; it's just a function that returns an r-value reference. So it makes it harder, not easier, for the compiler to "see through" and optimize away, even in the case where "as if" rule should make that legal.