220 11750 <b72f6510-4179-4369-afb0-c5ae27c0d404@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: tomaszkam@gmail.com
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: N4078: Rvalue reference overloads for value()
 method returns object by value
Date: Wed, 9 Jul 2014 12:21:54 -0700 (PDT)
Lines: 355
Approved: news@gmane.org
Message-ID: <b72f6510-4179-4369-afb0-c5ae27c0d404@isocpp.org>
References: <b538efba-faf4-4ffc-a553-302b9d2ba2c0@isocpp.org>
 <CANh-dX=M7niArNp-1sCcqxaREEJSxmjMftgw7uMHN9YnZC-h+w@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_335_18890479.1404933714826"
X-Trace: ger.gmane.org 1404933727 22653 80.91.229.3 (9 Jul 2014 19:22:07 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 9 Jul 2014 19:22:07 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDNPVXXG6IGBBU5M62OQKGQEFSS3AWI@isocpp.org Wed Jul 09 21:22:00 2014
Return-path: <std-proposals+bncBDNPVXXG6IGBBU5M62OQKGQEFSS3AWI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f71.google.com ([209.85.220.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDNPVXXG6IGBBU5M62OQKGQEFSS3AWI@isocpp.org>)
	id 1X4xRF-0007mL-QG
	for gclcip-std-proposals@m.gmane.org; Wed, 09 Jul 2014 21:21:58 +0200
Original-Received: by mail-pa0-f71.google.com with SMTP id eu11sf53243376pac.2
        for <gclcip-std-proposals@m.gmane.org>; Wed, 09 Jul 2014 12:21:56 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe
         :content-type;
        bh=aTiL91qAhwJZ+2PLbYmB1XatN01xVQ6sY1+nLo1VZ7Q=;
        b=dZVRwcemcFU0qAYpaTRqhxYJFLiRuJgadJwUwHwJA3IA7uY9BVU+e1dolg28hqyUW9
         QlYKeGbZWyvkaoYmHoG4brDQBBkzh3p0dd4y2eeOn8q1Zpk6C+swzbk8W0pHmcOdoCtC
         tOnUb1l7NKO8vw6DIeA3Rwyr3BXtL8OBsuXSXkSWUvTCPPmWJD2bjWwLNPmcMAlneDAn
         MH/136bM5/Ca4Jk6lEoSzIwtzad6lu8Ov+vL3vPi7+9xxy6zqRNUxik/G1T199cY69Vj
         +BF8Rmq49sdfLG3QpvxoJmocdCn3EsJYxb2px8tZywZwYpVHtpP93CE8gpSYUD4nn7vs
         lwHQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=aTiL91qAhwJZ+2PLbYmB1XatN01xVQ6sY1+nLo1VZ7Q=;
        b=FqjsIq1TW5pzQUTT9Oow1BRCz5/LZxN2vzIk7ceOi5ggDOi0+JG1gBpTassQkXQp4B
         EiPssy/lKLALN0+mPrSta44z2h86XVUEVmFRfaEVqB++h31BxmF5+Ymrjy4abdsxAokv
         PhPScp9BYGACAxXhOWYt41bbwP251xY8U+1Vi9XFgDC6b8XRKfB17i4XIYMvbRY6WQNd
         Bns0JeoY6kCQpphAghJ1MNTpaZim5zSq/30zQAFpWk78VNr9bxQarmm3eH8JZ1EUklub
         xofd/KpCd8ECSH5Wrv+uHhu8/3SNJ2CYZqKx+ikFhd/mOU4NcqkJxirjRTU1ap+qNs6C
         hmJQ==
X-Gm-Message-State: ALoCoQnppmklmNfVem76RFA71f+lDYnYrk/ZtZV7+t0ycUk/RXrei9LaAgR1G0rMViTmuk/vS84t
X-Received: by 10.66.155.226 with SMTP id vz2mr4808838pab.47.1404933716722;
        Wed, 09 Jul 2014 12:21:56 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.129.164 with SMTP id nx4ls1323914obb.88.gmail; Wed, 09 Jul
 2014 12:21:55 -0700 (PDT)
X-Received: by 10.182.109.234 with SMTP id hv10mr236719obb.2.1404933715497;
        Wed, 09 Jul 2014 12:21:55 -0700 (PDT)
In-Reply-To: <CANh-dX=M7niArNp-1sCcqxaREEJSxmjMftgw7uMHN9YnZC-h+w@mail.gmail.com>
X-Original-Sender: tomaszkam@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:11750
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/11750>

------=_Part_335_18890479.1404933714826
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



W dniu =C5=9Broda, 9 lipca 2014 21:09:36 UTC+2 u=C5=BCytkownik Jeffrey Yass=
kin=20
napisa=C5=82:
>
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1651=20
> addresses:
>
>   std::pair<std::string, exception_ptr> g();
>   auto&& s =3D g().first; //will create a dangling reference
>
> It's in 'ready' status, meaning it's not in C++14, but it's likely to get=
=20
> fixed immediately after, possibly as a "defect", which would mean that mo=
st=20
> compilers would implement the fix in their C++14 modes.
>
But it does not still fix the following:
  std::pair<std::string, exception_ptr> g();
  auto&& s =3D std::get<0>(g()); //will create a dangling reference
And I don not think that it is even possible to change meaning of the get=
=20
with lvalue because lot of code depends of that.
Or:
  auto f =3D std::mem_fn(A::member);
  /* ........... */
  auto&& s =3D f(g()); //will create a dangling reference
Actually I think taht the problem lies in the usage of auto&&, not in the=
=20
return types.

I agree that I'd rather see the general issue fixed by a language change=20
> (probably involving a change to the library to use the new language=20
> feature). I'm ambivalent about the safety fix we have in optional<>.
> =20
Actually just one other case from the library that come to my mind. Would=
=20
we want to make the following to do not create dangling reference:
    std::vector<std::string> f();
    auto&& s =3D *f.begin(); //dangling reference
This will require us the create a begin/end r-value overload that returns=
=20
same kind of copy-iterator that return by value when *it is invoked. I am=
=20
not
sure what consequence will it have.
=20

> On Jul 5, 2014 1:52 PM, <anna.s...@gmail.com <javascript:>> wrote:
>
>> In the paper N4078 <https://isocpp.org/files/papers/N4078.html> two=20
>> Rvalue reference overloads was added to the optional<T> class:
>>   constexpr T value() &&;
>>   constexpr T value() const&&
>> I think this overloads should return by r-value reference instead.=20
>>   constexpr T&& value() &&;
>>
>> *I am not aware of any other motivation for this change, please post if=
=20
>> other one exists.*
>> Lets begin with the motivation for current desing. As far as I know this=
=20
>> was introduced to make=20
>> following code well-behaved:
>>   optional<string> f();
>>   auto&& s =3D f().value(); //this code creates a dangling reference if =
we=20
>> would return by reference
>> I don think making this well-behaved makes more harm that good. It adds=
=20
>> single exceptional class
>> int the langugage for which such code is well behaved. For example if we=
=20
>> write very similiar code
>> with a vector or std::tuple, this code wil still create a dangling=20
>> reference.
>>   std::vector<std::string> f();
>>   auto&& s =3D f.front(); //dangling reference
>>   std::tuple<std::string, exception_ptr> g();
>>   auto&& s =3D get<0>(g()); //this is emulation of expected<T> or=20
>> optional<T> with tuple
>> So the standard library is not consistent with the behaviour, and even i=
f=20
>> we fix every getter method
>> in the standard by adding rvalue overload, the problem still won't be=20
>> fixed without changing language
>> in case of the members:
>>   std::pair<std::string, exception_ptr> g();
>>   auto&& s =3D g().first; //will create a dangling reference
>>
>> So to summarize:
>> Now we have single class in the standard that provides the rvalue=20
>> reference overload for getter method
>> and make the auto&& s =3D f().value() well defined. But this is not and=
=20
>> cannot be uniformly applied to
>> the rest of the language (because of member access). This in my opinion=
=20
>> makes language more complicated
>> and leave the programmer with two options:
>>   - remember this special case and apply them when possible
>>   - ignore existence of this overloads
>>
>> In addition the current desings introduces preformance impact on the=20
>> code. Let assume following:
>>     optional<T> f();
>>     void g(const T&);o
>>  =20
>>    vector<T> vt; vt.emplace_back(f.value());=20
>>    g(f().value());
>>    This following two lines will now introduce additional=20
>> move-construction o value of type T.
>>    Someone may argue that this cost is not large becase move are cheap=
=20
>> (for example std::string). But in the working codebase that
>>    is a lot of legacy classes that are not move-constructible and this=
=20
>> will introduce additional unecessary cost. Of course this
>>    problem does not exists for tuple or pairs.
>> =20
>> In my opinion it would be better to delcaret this functions as returning=
=20
>> reference because it will make it consistent with rest of the language
>> and by doing it will make it easier to use and understand.
>>
>> --=20
>>
>> ---=20
>> You received this message because you are subscribed to the Google Group=
s=20
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send a=
n=20
>> email to std-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> Visit this group at=20
>> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>>
>=20

--=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

------=_Part_335_18890479.1404933714826
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>W dniu =C5=9Broda, 9 lipca 2014 21:09:36 UTC+2 u=
=C5=BCytkownik Jeffrey Yasskin napisa=C5=82:<blockquote class=3D"gmail_quot=
e" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddin=
g-left: 1ex;"><div dir=3D"ltr"><p dir=3D"ltr"><a href=3D"http://www.open-st=
d.org/jtc1/sc22/wg21/docs/cwg_active.html#1651" target=3D"_blank" onmousedo=
wn=3D"this.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fwww.open-std.=
org%2Fjtc1%2Fsc22%2Fwg21%2Fdocs%2Fcwg_active.html%231651\46sa\75D\46sntz\07=
51\46usg\75AFQjCNHVoie10PZYFxvkX-eVFGjeRad_GQ';return true;" onclick=3D"thi=
s.href=3D'http://www.google.com/url?q\75http%3A%2F%2Fwww.open-std.org%2Fjtc=
1%2Fsc22%2Fwg21%2Fdocs%2Fcwg_active.html%231651\46sa\75D\46sntz\0751\46usg\=
75AFQjCNHVoie10PZYFxvkX-eVFGjeRad_GQ';return true;">http://www.open-std.org=
/jtc1/<wbr>sc22/wg21/docs/cwg_active.<wbr>html#1651</a> addresses:</p><p di=
r=3D"ltr"><span style=3D"font-family:arial,sans-serif">&nbsp; std::pair&lt;=
std::string, exception_ptr&gt; g();</span><br style=3D"font-family:arial,sa=
ns-serif">

<span style=3D"font-family:arial,sans-serif">&nbsp; auto&amp;&amp; s =3D g(=
).first; //will create a dangling reference</span><br></p><p>It's in 'ready=
' status, meaning it's not in C++14, but it's likely to get fixed immediate=
ly after, possibly as a "defect", which would mean that most compilers woul=
d implement the fix in their C++14 modes.</p></div></blockquote><div>But it=
 does not still fix the following:<br>&nbsp;<span style=3D"font-family:aria=
l,sans-serif"> std::pair&lt;std::string, exception_ptr&gt; g();</span><br s=
tyle=3D"font-family:arial,sans-serif">

<span style=3D"font-family:arial,sans-serif">&nbsp; auto&amp;&amp; s =3D st=
d::get&lt;0&gt;(g()); //will create a dangling reference</span><br>And I do=
n not think that it is even possible to change meaning of the get with lval=
ue because lot of code depends of that.<br>Or:<br>&nbsp; auto f =3D std::me=
m_fn(A::member);<br>&nbsp; /* ........... */<br>&nbsp; <span style=3D"font-=
family:arial,sans-serif">auto&amp;&amp; s =3D f(g()); //will create a dangl=
ing reference</span><br>Actually I think taht the problem lies in the usage=
 of auto&amp;&amp;, not in the return types.<br><br></div><blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;"><div dir=3D"ltr">

<p>I agree that I'd rather see the general issue fixed by a language change=
 (probably involving a change to the library to use the new language featur=
e). I'm ambivalent about the safety fix we have in optional&lt;&gt;.<br>

</p>
<div class=3D"gmail_quote"></div></div></blockquote><div>Actually just one =
other case from the library that come to my mind. Would we want to make the=
 following to do not create dangling reference:<br>&nbsp;&nbsp;&nbsp; std::=
vector&lt;std::string&gt; f();<br>&nbsp;&nbsp;&nbsp; auto&amp;&amp; s =3D *=
f.begin(); //dangling reference<br>This will require us the create a begin/=
end r-value overload that returns same kind of copy-iterator that return by=
 value when *it is invoked. I am not<br>sure what consequence will it have.=
<br>&nbsp;</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin=
-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"lt=
r"><div class=3D"gmail_quote">On Jul 5, 2014 1:52 PM,  &lt;<a href=3D"javas=
cript:" target=3D"_blank" gdf-obfuscated-mailto=3D"jqJB6k03cWAJ" onmousedow=
n=3D"this.href=3D'javascript:';return true;" onclick=3D"this.href=3D'javasc=
ript:';return true;">anna.s...@gmail.com</a>&gt; wrote:<br type=3D"attribut=
ion"><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bo=
rder-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:so=
lid;padding-left:1ex">


<div dir=3D"ltr"><span style=3D"font-family:arial,sans-serif">In the paper =
</span><a style=3D"font-family:arial,sans-serif" href=3D"https://isocpp.org=
/files/papers/N4078.html" target=3D"_blank" onmousedown=3D"this.href=3D'htt=
ps://www.google.com/url?q\75https%3A%2F%2Fisocpp.org%2Ffiles%2Fpapers%2FN40=
78.html\46sa\75D\46sntz\0751\46usg\75AFQjCNHCY0rGdgNwK1xSqCTZsz7l9ecf6g';re=
turn true;" onclick=3D"this.href=3D'https://www.google.com/url?q\75https%3A=
%2F%2Fisocpp.org%2Ffiles%2Fpapers%2FN4078.html\46sa\75D\46sntz\0751\46usg\7=
5AFQjCNHCY0rGdgNwK1xSqCTZsz7l9ecf6g';return true;">N4078</a><span style=3D"=
font-family:arial,sans-serif"> two Rvalue reference overloads was added to =
the optional&lt;T&gt; class:</span><br style=3D"font-family:arial,sans-seri=
f">


<code><span style=3D"font-family:arial,sans-serif">&nbsp; constexpr T value=
() &amp;&amp;;</span><br style=3D"font-family:arial,sans-serif"><span style=
=3D"font-family:arial,sans-serif">&nbsp; constexpr T value() const&amp;&amp=
;</span><br style=3D"font-family:arial,sans-serif">


<span style=3D"font-family:arial,sans-serif">I think this overloads should =
return by r-value reference instead. <br></span></code><code><span style=3D=
"font-family:arial,sans-serif">&nbsp; constexpr T&amp;&amp; value() &amp;&a=
mp;;</span></code><br>


<code><span style=3D"font-family:arial,sans-serif"><br><b>I am not aware of=
 any other motivation for this change, please post if other one exists.</b>=
<br>Lets begin with the motivation for current desing. As far as I know thi=
s was introduced to make <br>


following code well-behaved:<br>&nbsp; optional&lt;string&gt; f();<br>&nbsp=
; auto&amp;&amp; s =3D f().value(); //this code creates a dangling referenc=
e if we would return by reference<br>I don think making this well-behaved m=
akes more harm that good. It adds single exceptional class<br>


int the langugage for which such code is well behaved. For example if we wr=
ite very similiar code<br>with a vector or std::tuple, this code wil still =
create a dangling reference.<br>&nbsp; std::vector&lt;std::string&gt; f();<=
br>


&nbsp; auto&amp;&amp; s =3D f.front(); //dangling reference<br>&nbsp; std::=
tuple&lt;std::string, exception_ptr&gt; g();<br>&nbsp; auto&amp;&amp; s =3D=
 get&lt;0&gt;(g()); //this is emulation of expected&lt;T&gt; or optional&lt=
;T&gt; with tuple<br>


So the standard library is not consistent with the behaviour, and even if w=
e fix every getter method<br>in the standard by adding rvalue overload, the=
 problem still won't be fixed without changing language<br>in case of the m=
embers:<br>


</span></code><code><span style=3D"font-family:arial,sans-serif">&nbsp; std=
::pair&lt;std::string, exception_ptr&gt; g();<br>
&nbsp; auto&amp;&amp; s =3D g().first; //will create a dangling reference<b=
r><br>So to summarize:<br>Now we have single class in the standard that pro=
vides the rvalue reference overload for getter method<br>and make the auto&=
amp;&amp; s =3D f().value() well defined. But this is not and cannot be uni=
formly applied to<br>


the rest of the language (because of member access). This in my opinion mak=
es language more complicated<br>and leave the programmer with two options:<=
br>&nbsp; - remember this special case and apply them when possible<br>&nbs=
p; - ignore existence of this overloads<br>


<br>In addition the current desings introduces preformance impact on the co=
de. Let assume following:<br>&nbsp;&nbsp;&nbsp; optional&lt;T&gt; f();<br>&=
nbsp;&nbsp;&nbsp; void g(const T&amp;);o<br>&nbsp; <br>&nbsp;&nbsp; vector&=
lt;T&gt; vt; vt.emplace_back(f.value()); <br>


&nbsp;&nbsp; g(f().value());<br>&nbsp;&nbsp; This following two lines will =
now introduce additional move-construction o value of type T.<br>&nbsp;&nbs=
p; Someone may argue that this cost is not large becase move are cheap (for=
 example std::string). But in the working codebase that<br>


&nbsp;&nbsp; is a lot of legacy classes that are not move-constructible and=
 this will introduce additional unecessary cost. Of course this<br>&nbsp;&n=
bsp; problem does not exists for tuple or pairs.<br>&nbsp;<br>In my opinion=
 it would be better to delcaret this functions as returning reference becau=
se it will make it consistent with rest of the language<br>


and by doing it will make it easier to use and understand.<br></span></code=
></div>

<p></p>

-- <br>
<br>
--- <br>
You received this message because you are subscribed to the Google Groups "=
ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
jqJB6k03cWAJ" onmousedown=3D"this.href=3D'javascript:';return true;" onclic=
k=3D"this.href=3D'javascript:';return true;">std-proposal...@<wbr>isocpp.or=
g</a>.<br>
To post to this group, send email to <a href=3D"javascript:" target=3D"_bla=
nk" gdf-obfuscated-mailto=3D"jqJB6k03cWAJ" onmousedown=3D"this.href=3D'java=
script:';return true;" onclick=3D"this.href=3D'javascript:';return true;">s=
td-pr...@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank" onmousedown=3D"this.href=3D'http://groups=
..google.com/a/isocpp.org/group/std-proposals/';return true;" onclick=3D"thi=
s.href=3D'http://groups.google.com/a/isocpp.org/group/std-proposals/';retur=
n true;">http://groups.google.com/a/<wbr>isocpp.org/group/std-<wbr>proposal=
s/</a>.<br>
</blockquote></div>
</div>
</blockquote></div>

<p></p>

-- <br />
<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 <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 />
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_335_18890479.1404933714826--

.
