Programmers expectreturn x;
to trigger copy elision; or, at worst, to implicitly move fromx
instead of copying. Occasionally, C++ violates their expectations and performs an expensive copy anyway. Based on our experience using Clang to diagnose unexpected copies in Chromium, Mozilla, and LibreOffice, we propose to change the standard so that these copies will be replaced with implicit moves.
[...]
This feature has effectively already been implemented in Clang since February 2018; see [D43322]. Under the diagnostic option-Wreturn-std-move
(which is enabled as part of-Wmove
,-Wmost
, and-Wall
), the compiler performs overload resolution according to both rules — the standard rule and also the rule proposed in this proposal. If the two resolutions produce different results, then Clang emits a warning diagnostic explaining that the return value will not be implicitly moved and suggesting that the programmer add an explicitstd::move
.
Anyway, attempt #3. The slicing example is not compelling and should go last, if it even needs to be present at all. Much more compelling are by-value sink arguments:
struct A {
A(std::string);
};
struct B {
B(std::unique_ptr<int>);
};
A eleven() {
std::string s;
return s; // copy
}
B twelve() {
std::unique_ptr<int> p;
return p; // error
}
Would be nice if those both compiled and moved. Also, would be worth it to ping Howard about the original restriction... it's from way back in N1952.
Also, as a formatting note... would suggest highlighting instead of bold. The bold doesn't really stand out.
This is CWG 1579: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579
and was accepted for C++14.
Howard
On Aug 14, 2018, at 6:57 PM, Barry Revzin <barry....@gmail.com> wrote:
>
> I dunno why my messages keep getting deleted.
>
> Anyway, attempt #3. The slicing example is not compelling and should go last, if it even needs to be present at all. Much more compelling are by-value sink arguments:
>
> struct A {
> A(std::string);
> };
>
> struct B {
> B(std::unique_ptr<int>);
> };
>
> A eleven() {
> std::string s;
> return s; // copy
> }
>
> B twelve() {
> std::unique_ptr<int> p;
> return p; // error
> }
>
> Would be nice if those both compiled and moved. Also, would be worth it to ping Howard about the original restriction... it's from way back in N1952.
>
> Also, as a formatting note... would suggest highlighting instead of bold. The bold doesn't really stand out.
>
> --
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/b2f16fed-730b-402c-bffb-eb4dc8f3ed69%40isocpp.org.
--
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/eeLS8vI05nM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/98C0A9E1-E6BB-4E21-BA9A-A88FD45E9440%40gmail.com.
This is CWG 1579: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579
and was accepted for C++14.
Howard
struct A {
A(std::string&&);
};
A f() {
std::string x;
return x; // move because of this issue
}
Thank you for the education! I had no idea the rules were this screwed up.
I'm attaching a new draft revision of D1155 "More implicit moves."