220 11726 <764569c3-b26b-4938-970e-44a4681de132@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: =?UTF-8?Q?Andrzej_Krzemie=C5=84ski?= <akrzemi1@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 04:21:43 -0700 (PDT)
Lines: 206
Approved: news@gmane.org
Message-ID: <764569c3-b26b-4938-970e-44a4681de132@isocpp.org>
References: <b538efba-faf4-4ffc-a553-302b9d2ba2c0@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_125_22173508.1404904903166"
X-Trace: ger.gmane.org 1404904912 26964 80.91.229.3 (9 Jul 2014 11:21:52 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 9 Jul 2014 11:21:52 +0000 (UTC)
Cc: anna.salwa.05@gmail.com
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDT2DGOJ34DBBSGL6SOQKGQEU5ALBAA@isocpp.org Wed Jul 09 13:21:46 2014
Return-path: <std-proposals+bncBDT2DGOJ34DBBSGL6SOQKGQEU5ALBAA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ie0-f198.google.com ([209.85.223.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDT2DGOJ34DBBSGL6SOQKGQEU5ALBAA@isocpp.org>)
	id 1X4pwX-0004Yz-Ol
	for gclcip-std-proposals@m.gmane.org; Wed, 09 Jul 2014 13:21:46 +0200
Original-Received: by mail-ie0-f198.google.com with SMTP id lx4sf29234604iec.9
        for <gclcip-std-proposals@m.gmane.org>; Wed, 09 Jul 2014 04:21:44 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:cc: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=BZWFGZqdRy4ZE7Nl28/FrJWaTN3y5/W/ECd7req52eE=;
        b=jKPXLRWiEKjNOpRTuXGS9y4HW2EkKgu3FO2540e1TTyL7zk7nljUuePKHwRpBCqeNJ
         fnWGr+qi/lNIG3UfPp+hIVvT19u1huGCfbK+TcuJkE32yKopM9dFboOblirGJC4hDHqk
         cty5pFdxUWhU4Jc6j1vtU4sog8J54BdwGuSkSj1Va0LpKSKwvq36MlEhu+ljuIxgzugH
         P92NN7H/+MLktE8jVce/IpgiW35HWFpo+mC+Ra/2ERzImu3xEhfVzs11vWZUoegcv8zR
         Bu/A4ysLVmmOPGu/CEIWM1L+Bi6TdSKo7XAstpSixVegRsSbXpVu2lXLonEu1skGOdFG
         h8Yg==
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:cc: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=BZWFGZqdRy4ZE7Nl28/FrJWaTN3y5/W/ECd7req52eE=;
        b=Q3RROCAlY3s9Ad/9r3wVIgqeOV/tcIYNocBaIUoxu3fyjKkqi85qKcINWZamLWxJ6P
         gnfKBaK5oBruGQ6qza2gveMipdYDwh1i2VdBWVs5kE0Lj594vORK8t0gDfLKOAS5uMfo
         UiG9D5ZpUDMCJmW+9wZAOGaipcc/lWszZVLN4W3Fi8bVmJL2VZtTOJvdRkw1NJ6mzVc6
         g+G7YQzr095VKoI5DYuO3uvCjS7329+TzMraRjoDrMJz5iUIa9KRc3+vRK45vdVEvhOU
         bGAdZkkpoTOrbdhAa1m2LE55Mkkb3IgKkx/AvRxLLNfPqVhpmvnG6clAgp1Wh833Ynss
         pF+g==
X-Gm-Message-State: ALoCoQnmRdpMLwITtK72sOhHzr64npSO6NZvkzILUhu3hTwiRYWJGYZiG02fsYCvcwiFsMXIO3C0
X-Received: by 10.182.28.5 with SMTP id x5mr10135506obg.44.1404904904861;
        Wed, 09 Jul 2014 04:21:44 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.42.195 with SMTP id q3ls1180961obl.97.gmail; Wed, 09 Jul
 2014 04:21:44 -0700 (PDT)
X-Received: by 10.182.226.233 with SMTP id rv9mr209955obc.6.1404904904087;
        Wed, 09 Jul 2014 04:21:44 -0700 (PDT)
In-Reply-To: <b538efba-faf4-4ffc-a553-302b9d2ba2c0@isocpp.org>
X-Original-Sender: akrzemi1@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:11726
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/11726>

------=_Part_125_22173508.1404904903166
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



W dniu sobota, 5 lipca 2014 22:52:06 UTC+2 u=C5=BCytkownik anna.s...@gmail.=
com=20
napisa=C5=82:
>
> 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 w=
e=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 if=
=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 code=
..=20
> 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.
>

The only motivation for returning by value -- to the best of my knowledge=
=20
-- is the avoidance of dangling references in certain cases. My=20
understanding is that LEWG preferred these to return by value, even at the=
=20
expense of incurring run-time overhead. My personal motivation was to=20
minimize the controversy with LEWG and increasing the chances of getting=20
the proposal through. (Rvalue ref overloads returning by value are better=
=20
than no rvalue ref overloads.)

I hope LEWG members are reading this list and can shed some more light on=
=20
this question.

Regards,
&rzej

--=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_125_22173508.1404904903166
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>W dniu sobota, 5 lipca 2014 22:52:06 UTC+2 u=C5=BC=
ytkownik anna.s...@gmail.com napisa=C5=82:<blockquote class=3D"gmail_quote"=
 style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-=
left: 1ex;"><div dir=3D"ltr"><span style=3D"font-family:arial,sans-serif">I=
n 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'https://www.google.com/url?q\75https%3A%2F%2Fisocpp.org%2Ffiles%2F=
papers%2FN4078.html\46sa\75D\46sntz\0751\46usg\75AFQjCNHCY0rGdgNwK1xSqCTZsz=
7l9ecf6g';return 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\75AFQjCNHCY0rGdgNwK1xSqCTZsz7l9ecf6g';return true;">N4078</a><sp=
an style=3D"font-family:arial,sans-serif"> two Rvalue reference overloads w=
as added to the optional&lt;T&gt; class:</span><br style=3D"font-family:ari=
al,sans-serif"><code><span style=3D"font-family:arial,sans-serif">&nbsp; co=
nstexpr T value() &amp;&amp;;</span><br style=3D"font-family:arial,sans-ser=
if"><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 sty=
le=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-fam=
ily:arial,sans-serif">&nbsp; constexpr T&amp;&amp; value() &amp;&amp;;</spa=
n></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 on=
e exists.</b><br>Lets begin with the motivation for current desing. As far =
as I know this 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 reference if we would return by reference=
<br>I don think making this well-behaved makes more harm that good. It adds=
 single exceptional class<br>int the langugage for which such code is well =
behaved. For example if we write very similiar code<br>with a vector or std=
::tuple, this code wil still create a dangling reference.<br>&nbsp; std::ve=
ctor&lt;std::string&gt; f();<br>&nbsp; auto&amp;&amp; s =3D f.front(); //da=
ngling 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 librar=
y is not consistent with the behaviour, and even if we fix every getter met=
hod<br>in the standard by adding rvalue overload, the problem still won't b=
e fixed without changing language<br>in case of the members:<br></span></co=
de><code><span style=3D"font-family:arial,sans-serif">&nbsp; std::pair&lt;s=
td::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). T=
his in my opinion makes language more complicated<br>and leave the programm=
er with two options:<br>&nbsp; - remember this special case and apply them =
when possible<br>&nbsp; - ignore existence of this overloads<br><br>In addi=
tion the current desings introduces preformance impact on the code. Let ass=
ume 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;&nb=
sp; This following two lines will now introduce additional move-constructio=
n o value of type T.<br>&nbsp;&nbsp; Someone may argue that this cost is no=
t 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 cours=
e this<br>&nbsp;&nbsp; problem does not exists for tuple or pairs.<br>&nbsp=
;<br>In my opinion it would be better to delcaret this functions as returni=
ng reference because it will make it consistent with rest of the language<b=
r>and by doing it will make it easier to use and understand.<br></span></co=
de></div></blockquote><div><br>The only motivation for returning by value -=
- to the best of my knowledge -- is the avoidance of dangling references in=
 certain cases. My understanding is that LEWG preferred these to return by =
value, even at the expense of incurring run-time overhead. My personal moti=
vation was to minimize the controversy with LEWG and increasing the chances=
 of getting the proposal through. (Rvalue ref overloads returning by value =
are better than no rvalue ref overloads.)<br><br>I hope LEWG members are re=
ading this list and can shed some more light on this question.<br><br>Regar=
ds,<br>&amp;rzej<br></div></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_125_22173508.1404904903166--

.
