Topic: Copy elision and replacing copy by move in


Author: Mikhail Semenov <mikhailsemenov1957@gmail.com>
Date: Sun, 5 Jan 2014 05:31:44 -0800 (PST)
Raw View
------=_Part_990_28422441.1388928704013
Content-Type: text/plain; charset=ISO-8859-1


In the following cases it is natural to allow copy elision to take place:

class A
{
     double *v;
     std::size_t n;


public:
.. . .
        A operator+(A x) const &
        {
                for (std::size_t i = 0; i < n; i++)
                {
                        x.v[i] += v[i];
                }
                return x; // copy elision will take place here
        }

         A operator+(const A& x) && // rvalue-ref-qualified
         {
                 for (std::size_t i = 0; i < n; i++)
                 {
                         v[i] += x.v[i];
                 }
                 return std::move(*this); // At present, std::move is
required here in order to get the desired effect. But it seems that  return
*this; should be enough.
         }
.. . .
};


There are two sides for this proposal:

-- when a member function returns a whole object of its class then a copy
elision can take place;

-- when a member function returns a member variable, then copy can be
replaced with move.

Here is another example:

*class P*

*{*

*    S r;*

*    . . .*

*public:*

*    P();*

*    . . .*

*    S f() &&*

*    {*

*       . . .*

*       return a.r; // copy is replaced by move*

*    }*

*};*



*S w = P().f();*



In this case, the value *a.r* can be moved into *w*, not copied.



This proposal is available here:

*https://dl.dropboxusercontent.com/u/35715999/copy_elision_for_r_value_qualified_members.htm*<https://dl.dropboxusercontent.com/u/35715999/copy_elision_for_r_value_qualified_members.htm>





--

---
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-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_990_28422441.1388928704013
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>&nbsp;</div><div>In the following cases it is natural=
 to allow copy elision to take place: </div><div>&nbsp;</div><div>class A<b=
r> {<br> &nbsp;&nbsp;&nbsp;&nbsp; double *v;<br> &nbsp;&nbsp;&nbsp;&nbsp; s=
td::size_t n; <br><br><br> public:<br> . . .<br> &nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp; A operator+(A x) const &amp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (std::size_t i =3D 0; i &lt; n;=
 i++)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; x.v[i] +=3D v[i];<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp; return x; // copy elision will take place here<br> &nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; }<br><br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; <font color=3D"#0000ff">A operator+(const A&amp; x) &amp;&amp; // =
rvalue-ref-qualified<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {=
 <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; for (std::size_t i =3D 0; i &lt; n; i++)<br> &n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp; v[i] +=3D x.v[i];<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp; return std::move(*this); // At present, std::move is requ=
ired here in order to get the desired effect. But it seems that &nbsp;<font=
 color=3D"#274e13">return *this;</font> should be enough.<br> &nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</font><br> . . .<br> }; </div><d=
iv>&nbsp;</div><div><font color=3D"#000000" face=3D"Times New Roman" size=
=3D"3">

</font></div><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 8pt;"><font si=
ze=3D"3"><font color=3D"#000000"><font face=3D"Times New Roman">There are t=
wo sides for this proposal: </font></font></font></p><p class=3D"MsoNormal"=
 style=3D"margin: 0cm 0cm 8pt;"><font size=3D"3"><font color=3D"#000000"><f=
ont face=3D"Times New Roman">-- when a member function returns a whole obje=
ct of its
class then a copy elision can take place;<?xml:namespace prefix =3D "o" ns =
=3D "urn:schemas-microsoft-com:office:office" /><o:p></o:p></font></font></=
font></p><div><font color=3D"#000000" face=3D"Times New Roman" size=3D"3">
</font></div><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 8pt;"><font si=
ze=3D"3"><font color=3D"#000000"><font face=3D"Times New Roman">-- when a m=
ember function returns a member variable, then
copy can be replaced with move.<o:p></o:p></font></font></font></p><div><fo=
nt color=3D"#000000" face=3D"Times New Roman" size=3D"3">
</font></div><div><font color=3D"#000000" face=3D"Times New Roman" size=3D"=
3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 8pt;"><font size=3D"=
3"><font color=3D"#000000"><font face=3D"Times New Roman">Here is another&n=
bsp;example:<o:p></o:p></font></font></font></p><font color=3D"#000000" fac=
e=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>class P</span></b><o:p></o:p></font></p><font color=3D"=
#000000" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>{</span></b><o:p></o:p></font></p><font color=3D"#00000=
0" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
S r;</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times =
New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
.. . .</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times=
 New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>public:</span></b><o:p></o:p></font></p><font color=3D"=
#000000" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
P();</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times =
New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
.. . .</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times=
 New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
S f() &amp;&amp;</span></b><o:p></o:p></font></p><font color=3D"#000000" fa=
ce=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
{</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times New=
 Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
.. . .</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times=
 New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><b><span style=3D'font-family: "Courier New"; font-size: 11pt;'><fon=
t color=3D"#000000">&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;</font><span style=3D"background: black; color: white;">return =
a.r</span><font color=3D"#000000">; //
copy is replaced by move</font></span></b><o:p></o:p></p><font color=3D"#00=
0000" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;&nbsp;&nbsp;
}</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Times New=
 Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>};</span></b><o:p></o:p></font></p><font color=3D"#0000=
00" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>&nbsp;</span></b><o:p></o:p></font></p><font color=3D"#=
000000" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font color=3D"#000000"><b><span style=3D'font-family: "Courier New"=
; font-size: 11pt;'>S w =3D
P().f();</span></b><o:p></o:p></font></p><font color=3D"#000000" face=3D"Ti=
mes New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font size=3D"3"><font color=3D"#000000"><font face=3D"Times New Rom=
an">&nbsp;<o:p></o:p></font></font></font></p><font color=3D"#000000" face=
=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 0pt; line-height: no=
rmal;"><font size=3D"3"><font color=3D"#000000"><font face=3D"Times New Rom=
an">In this case, the value <b>a.r</b> can be moved into <b>w</b>, not
copied.</font></font></font></p><p class=3D"MsoNormal" style=3D"margin: 0cm=
 0cm 0pt; line-height: normal;"><font size=3D"3"><font color=3D"#000000"><f=
ont face=3D"Times New Roman"></font></font></font>&nbsp;</p><p class=3D"Mso=
Normal" style=3D"margin: 0cm 0cm 0pt; line-height: normal;"><font size=3D"3=
"><font color=3D"#000000"><font face=3D"Times New Roman">This proposal is a=
vailable here:</font></font></font></p><p class=3D"MsoNormal" style=3D"marg=
in: 0cm 0cm 0pt; line-height: normal;"><a onmousedown=3D"this.href=3D'https=
://www.google.com/url?q\75https%3A%2F%2Fdl.dropboxusercontent.com%2Fu%2F357=
15999%2Fcopy_elision_for_r_value_qualified_members.htm\46sa\75D\46sntz\0751=
\46usg\75AFQjCNHazxh1EhnflBbFqlpJPeuiVbyBFw';return true;" onclick=3D"this.=
href=3D'https://www.google.com/url?q\75https%3A%2F%2Fdl.dropboxusercontent.=
com%2Fu%2F35715999%2Fcopy_elision_for_r_value_qualified_members.htm\46sa\75=
D\46sntz\0751\46usg\75AFQjCNHazxh1EhnflBbFqlpJPeuiVbyBFw';return true;" hre=
f=3D"https://dl.dropboxusercontent.com/u/35715999/copy_elision_for_r_value_=
qualified_members.htm" target=3D"_blank"><u><font color=3D"#0000ff">https:/=
/dl.dropboxusercontent.<wbr>com/u/35715999/copy_elision_<wbr>for_r_value_qu=
alified_members.<wbr>htm</font></u></a></p><p class=3D"MsoNormal" style=3D"=
margin: 0cm 0cm 0pt; line-height: normal;">&nbsp;</p><font color=3D"#000000=
" face=3D"Times New Roman" size=3D"3">

</font><p class=3D"MsoNormal" style=3D"margin: 0cm 0cm 8pt;"><font size=3D"=
3"><font color=3D"#000000"><font face=3D"Times New Roman">&nbsp;<o:p></o:p>=
</font></font></font></p><font color=3D"#000000" face=3D"Times New Roman" s=
ize=3D"3">

</font></div></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_990_28422441.1388928704013--

.