Topic: Proposal for ref-qualifying the implicitly generated


Author: Jon Kalb <jon@kalbweb.com>
Date: Sun, 25 Mar 2018 18:23:07 -0700
Raw View
> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3604846990_1391288551
Content-type: text/plain; charset="UTF-8"
Content-transfer-encoding: quoted-printable

=20

Greetings,

=20

I=E2=80=99m working on a proposal to make the implicitly generated assignme=
nt declarations be ref-qualified so that they only work on lvalue objects.

=20

My thinking is that assigning to a temporary is never a good idea and any c=
ode which does this is likely not doing what the author intended, so it sho=
uld be caught by the compiler.

=20

The compiler will catch errors like this for fundamental types:

=20

int a{}, b{}, c{};

=20

a + b =3D c;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // compile error

=20

But allows this for user-defined types that use the implicitly declared ass=
ignment operator:

=20

UDT a{}, b{}, c{};

=20

a + b =3D c;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Not a compile error, but an unca=
ught logic error.

if ( (a + b) =3D c ) =E2=80=A6=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Did you mean =
=3D=3D? As is, this compiles (assuming UDT converts to int).

=20

My proposal is that the signatures of the implicitly generated assignment o=
perators be changed from this:

=20

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 X& X::operator=3D(X const&) or X& X::operator=3D(X&)=20

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 And

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 X& X::operator=3D(X &&)

=20

to this:

=20

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 X& X::operator=3D(X const&) & or X& X::operator=3D(X&) &

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 And

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 X& X::operator=3D(X &&) &

=20

=20


My early rough draft of the proposal is here:

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 http://exceptionsafecode.com/data/Don't%20Assign%20to%20Tem=
poraries.html

=20

Thanks.

=20

Jon

=20

--=20
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 e=
mail to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp=
..org/d/msgid/std-proposals/32DA2B8D-AF3F-42FC-AF24-58BA5612622A%40kalbweb.c=
om.

--B_3604846990_1391288551
Content-type: text/html; charset="UTF-8"
Content-transfer-encoding: quoted-printable

<html xmlns:o=3D"urn:schemas-microsoft-com:office:office" xmlns:w=3D"urn:sc=
hemas-microsoft-com:office:word" xmlns:m=3D"http://schemas.microsoft.com/of=
fice/2004/12/omml" xmlns=3D"http://www.w3.org/TR/REC-html40"><head><meta na=
me=3DTitle content=3D""><meta name=3DKeywords content=3D""><meta http-equiv=
=3DContent-Type content=3D"text/html; charset=3Dutf-8"><meta name=3DGenerat=
or content=3D"Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
 {font-family:"Cambria Math";
 panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
 {font-family:Calibri;
 panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
 {margin:0in;
 margin-bottom:.0001pt;
 font-size:12.0pt;
 font-family:Calibri;}
a:link, span.MsoHyperlink
 {mso-style-priority:99;
 color:#0563C1;
 text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
 {mso-style-priority:99;
 color:#954F72;
 text-decoration:underline;}
span.EmailStyle17
 {mso-style-type:personal-compose;
 font-family:Calibri;
 color:windowtext;}
span.msoIns
 {mso-style-type:export-only;
 mso-style-name:"";
 text-decoration:underline;
 color:teal;}
..MsoChpDefault
 {mso-style-type:export-only;
 font-family:Calibri;}
@page WordSection1
 {size:8.5in 11.0in;
 margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
 {page:WordSection1;}
--></style></head><body bgcolor=3Dwhite lang=3DEN-US link=3D"#0563C1" vlink=
=3D"#954F72"><div class=3DWordSection1><p class=3DMsoNormal><span style=3D'=
font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal><span st=
yle=3D'font-size:11.0pt'>Greetings,<o:p></o:p></span></p><p class=3DMsoNorm=
al><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3D=
MsoNormal><span style=3D'font-size:11.0pt'>I&#8217;m working on a proposal =
to make the implicitly generated assignment declarations be ref-qualified s=
o that they only work on lvalue objects.<o:p></o:p></span></p><p class=3DMs=
oNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p cla=
ss=3DMsoNormal><span style=3D'font-size:11.0pt'>My thinking is that assigni=
ng to a temporary is never a good idea and any code which does this is like=
ly not doing what the author intended, so it should be caught by the compil=
er.<o:p></o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0=
pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal><span style=3D'font-si=
ze:11.0pt'>The compiler will catch errors like this for fundamental types:<=
o:p></o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'>=
<o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal style=3D'text-indent:.5in'=
><span style=3D'font-size:11.0pt'>int a{}, b{}, c{};<o:p></o:p></span></p><=
p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></spa=
n></p><p class=3DMsoNormal style=3D'text-indent:.5in'><span style=3D'font-s=
ize:11.0pt'>a + b =3D c;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // compile error<o:p></=
o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&=
nbsp;</o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'=
>But allows this for user-defined types that use the implicitly declared as=
signment operator:<o:p></o:p></span></p><p class=3DMsoNormal><span style=3D=
'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal style=
=3D'text-indent:.5in'><span style=3D'font-size:11.0pt'>UDT a{}, b{}, c{};<o=
:p></o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><=
o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal style=3D'text-indent:.5in'>=
<span style=3D'font-size:11.0pt'>a + b =3D c;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 //=
 Not a compile error, but an uncaught logic error.<o:p></o:p></span></p><p =
class=3DMsoNormal style=3D'text-indent:.5in'><span style=3D'font-size:11.0p=
t'>if ( (a + b) =3D c ) &#8230;=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // Did you mea=
n =3D=3D? As is, this compiles (assuming UDT converts to int).<o:p></o:p></=
span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;<=
/o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'>My pr=
oposal is that the signatures of the implicitly generated assignment operat=
ors be changed from this:<o:p></o:p></span></p><p class=3DMsoNormal><span s=
tyle=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal>=
<span style=3D'font-size:11.0pt'>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 X&amp; X::operator=3D(X co=
nst&amp;) or X&amp; X::operator=3D(X&amp;) <o:p></o:p></span></p><p class=
=3DMsoNormal><span style=3D'font-size:11.0pt'>=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 And<o:p></o=
:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'>=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 X&amp; X::operator=3D(X &amp;&amp;)<o:p></o:p></span></p><p class=
=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><=
p class=3DMsoNormal><span style=3D'font-size:11.0pt'>to this:<o:p></o:p></s=
pan></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</=
o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'>=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 X&amp; X::operator=3D(X const&amp;) &amp; or X&amp; X::operator=
=3D(X&amp;) &amp;<o:p></o:p></span></p><p class=3DMsoNormal><span style=3D'=
font-size:11.0pt'>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 And<o:p></o:p></span></p><p class=3DMsoNo=
rmal><span style=3D'font-size:11.0pt'>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 X&amp; X::operator=
=3D(X &amp;&amp;) &amp;<o:p></o:p></span></p><p class=3DMsoNormal><span sty=
le=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNormal><s=
pan style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p class=3DMsoNo=
rmal><span style=3D'font-size:11.0pt'><br>My early rough draft of the propo=
sal is here:<o:p></o:p></span></p><p class=3DMsoNormal><span style=3D'font-=
size:11.0pt'>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 <a href=3D"http://exceptionsafecode.com/data=
/Don't%20Assign%20to%20Temporaries.html">http://exceptionsafecode.com/data/=
Don't%20Assign%20to%20Temporaries.html</a><o:p></o:p></span></p><p class=3D=
MsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p></span></p><p c=
lass=3DMsoNormal><span style=3D'font-size:11.0pt'>Thanks.<o:p></o:p></span>=
</p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&nbsp;</o:p>=
</span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'>Jon<o:p></=
o:p></span></p><p class=3DMsoNormal><span style=3D'font-size:11.0pt'><o:p>&=
nbsp;</o:p></span></p></div></body></html>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/32DA2B8D-AF3F-42FC-AF24-58BA5612622A%=
40kalbweb.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google=
..com/a/isocpp.org/d/msgid/std-proposals/32DA2B8D-AF3F-42FC-AF24-58BA5612622=
A%40kalbweb.com</a>.<br />

--B_3604846990_1391288551--



.