5

Regarding the proposal "Simpler implicit move" (P2266R1), I'm not sure if I understand this new "move-eligible" things correctly.

Please correct these points if incorrect:
[LIVE]

  1. std::forward becomes optional for perfect forwarding the rvalue ref received
template<class T>
T&& seven(T&& x) { return std::forward<T&&>(x); }

becomes

template<class T>
T&& seven(T&& x) { return x; }
  1. std::move become optional for rvalue ref created locally
Widget&&
test_seven(Widget w) {
    Widget&& rr = seven(std::move(w));
    return std::move(rr);
}

becomes

Widget&&
test_seven(Widget w) {
    Widget&& rr = seven(std::move(w));
    return rr;
}
  1. std::move optionaly becomes parenthesis only for return an rvalue ref for things created locally.
Widget&& h3(Widget t) {
  return std::move(t);
}

becomes

Widget&& h3(Widget t) {
  return (t);
}

Note: (3) : clang trunk warns of returning a local stack address at the time I post this.


Update 2021-08-02

https://github.com/cplusplus/papers/issues/968#issuecomment-915353127

https://isocpp.org/files/papers/P1018R13.html#P2266r1

Poll outcome: ✅ consensus. However, the against votes are from implementors, and bring relevant new information. This topic needs to be re-discussed, and might fail a plenary vote.

sandthorn
  • 2,770
  • 1
  • 15
  • 59
  • Interesting proposal. It'd be nice if you could add the [tag:language-lawyer] tag but I'm not sure which one of the other tags it should replace. – Ted Lyngmo Jul 12 '21 at 06:27
  • If you have questions about the proposal, it would probably be best to email Arthur directly. He's the one who understands the most about his proposal. Alternatively, Arthur is very active on the Cpplang Slack. – Justin Jul 12 '21 at 06:48
  • I understand like you. (And indeed `h3` returns dangling pointer (as `test_seven` and some of the examples in paper)). – Jarod42 Jul 12 '21 at 10:51

1 Answers1

5

All three of the points are correct. In all cases, the variable in question is an implicitly movable entity (except seven if instantiated with an lvalue) and thus is treated as an xvalue.

The parentheses here:

Widget&& h3(Widget t) {
  return (t);
}

don't actually do anything. They would if the function returned decltype(auto) - since then without parentheses the function would return Widget (but still move t, not copy it).

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Is that copy elision on the last point in your answer? If so, do both `Widget` and `auto` behave the same as `decltype(auto)` you mentioned? – sandthorn Jul 13 '21 at 05:22
  • 1
    @sandthorn There's no copy elision here. In `h3` as written, returning `Widget` and `auto` does the same thing (both return `Widget`) but `decltype(auto)` would return `Widget&&` with the parenthesized `t`. – Barry Jul 13 '21 at 13:24
  • One more thing, the clang's warning of returning local address is correct or clang needs to be fixed not to warn of this? – sandthorn Jul 13 '21 at 13:46
  • @sandthorn For `h3`? Definitely correct warning. That function is 100% returning a dangling reference. – Barry Jul 13 '21 at 15:13
  • However, I understand that [P2266R1](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2266r1.html) will allow h3 return a local scope address. h3 will be added into non-normative of [\[class.copy.elision\]/4](https://eel.is/c++draft/class.copy.elision#4). The standard allows returning local addresses or I miss anyting? – sandthorn Jul 14 '21 at 03:25
  • 1
    @sandthorn It's an example that helps demonstrate the language rule here. But we're still returning a reference to a local variable that is immediately being destroyed, so it's still a dangling reference. – Barry Jul 14 '21 at 13:07
  • Why does `return move(x);` not warn of local address returning while `return (x);` does? [LIVE](https://compiler-explorer.com/z/dx6eKzo53) – sandthorn Jul 17 '21 at 03:16
  • @sandthorn I don't understand, your link shows a warning on `move(x)` – Barry Jul 17 '21 at 15:13
  • In the middle section, you have GCC in which, of course, there sure is a warning. GCC doesn't support P2266R1 at the time we are now. In the right section, clang trunk shows no warnings. – sandthorn Jul 18 '21 at 17:01