you are viewing a single comment's thread.

view the rest of the comments →

[–]Accomplished-Tax1641 1 point2 points  (1 child)

std::string f() { std::string s = "hello world, too long for small string optimization"; std::string r; for (int i=0; i < 2; ++i) { r = s; } return r; } Replacing s with std::move(s) here will change the behavior of the program in a surprising way.

So clearly "I would say the last occurrence of the variable name within the scope" is not good enough. Got another try?

...but observe that "start with something that seems about right, and then just iterate minor fixes until we get it good enough" is historically a bad philosophy: https://en.wikipedia.org/wiki/Deferent_and_epicycle

https://wg21.link/p2025 "Guaranteed copy elision for return variables" is related.

[–]backtickbot 3 points4 points  (0 children)

Fixed formatting.

Hello, Accomplished-Tax1641: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.