220 11587 <7f41ece4-d9a3-47f7-ae42-c67fd39cac6f@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Krzysztof Ostrowski <freejazz@tlen.pl>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Teachability problems with std::move
Date: Fri, 27 Jun 2014 11:22:10 -0700 (PDT)
Lines: 207
Approved: news@gmane.org
Message-ID: <7f41ece4-d9a3-47f7-ae42-c67fd39cac6f@isocpp.org>
References: <CA+cyFgsYFbjG1M98PJS-XVzuJYbQmt7=4GOqS5_V5-WVJ0caLg@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_819_28922083.1403893330951"
X-Trace: ger.gmane.org 1403893342 20743 80.91.229.3 (27 Jun 2014 18:22:22 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 27 Jun 2014 18:22:22 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCGODRMA7YNBBVHMW2OQKGQEZOWDBPA@isocpp.org Fri Jun 27 20:22:15 2014
Return-path: <std-proposals+bncBCGODRMA7YNBBVHMW2OQKGQEZOWDBPA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-oa0-f71.google.com ([209.85.219.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCGODRMA7YNBBVHMW2OQKGQEZOWDBPA@isocpp.org>)
	id 1X0amr-0004N4-MA
	for gclcip-std-proposals@m.gmane.org; Fri, 27 Jun 2014 20:22:13 +0200
Original-Received: by mail-oa0-f71.google.com with SMTP id n16sf30952159oag.10
        for <gclcip-std-proposals@m.gmane.org>; Fri, 27 Jun 2014 11:22:12 -0700 (PDT)
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=84vBgVQTBTdgzQJoTPDEHOWmd91sz9E6ntMd3xTmgaw=;
        b=ODMcdkSjWNFJslg0Spq5PkbCcK1/FVoF3qEFOQmglI3OupUQCEM161QWdRGeEpCtpS
         tR120FxQ1Or+z6mNYrZYdJJMccaMOxoK/q8YSrnBEjqd6zRykEjmoEnq9mjsxuIKEZIn
         3JXOcvfNqeiI73DrwyBLiba62BvVI25vYE4p2xICUkLeV44aAc+l+T3FMzgmnupsUIQi
         ACwVvwWJ4qOjvseBRTJQTgWJUqL/aluPfsH8ofOk9kGxLboLNLGnJqy+GiEqEj7XfJLh
         5exyM9imaGBt4blih1037VXfXR7ix5FcWH70HuyRwmBv8iMtW5HTHMXtgEtUeV39YAok
         3PMQ==
X-Gm-Message-State: ALoCoQlutOps8VqWQcE3OGB5ySLj63Fn5yuo4TFUUQXkKLBKFrY68mGXEGy3/hpQ+IvevXIpZkpJ
X-Received: by 10.51.17.10 with SMTP id ga10mr6684173igd.4.1403893332714;
        Fri, 27 Jun 2014 11:22:12 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.105.136 with SMTP id c8ls648719qgf.76.gmail; Fri, 27 Jun
 2014 11:22:11 -0700 (PDT)
X-Received: by 10.140.51.18 with SMTP id t18mr23052qga.27.1403893331894;
        Fri, 27 Jun 2014 11:22:11 -0700 (PDT)
In-Reply-To: <CA+cyFgsYFbjG1M98PJS-XVzuJYbQmt7=4GOqS5_V5-WVJ0caLg@mail.gmail.com>
X-Original-Sender: freejazz@tlen.pl
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:11587
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/11587>

------=_Part_819_28922083.1403893330951
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable


I learnt that you should apply std::move only to the types that hold=20
resources (like heap memory) behind the scenes. That means you shouldn't=20
apply std::move to fundamental types and types that are bitwise copyable=20
(with shallow copy operation like aggregates). In fact, this applies to=20
std::array as well, but is limited to the implementation of type stored in=
=20
such array. Typical implementation of std::array might be:


template<class T, size_t n>
array { T storage[n]; /* some non-virual member function here... */  }

What if T is not bitwise copyable or it is, for instance std::vector<U> of=
=20
thousands of U? Should I apply move operation to every element of such=20
std::array<std::vector<U>, n>?  It depends mainly on N/RVO optimisations.=
=20
To be strict, moving blindly may not be a good idea.

Issuing a compiler warning on possible pessimization when applying=20
std::move is reasonable. Implicit conversion are evil in my opinion and=20
such conversion that changes properties of a type (like CV-qualifiers)=20
should result in warning. Compiler warnings are to help developers.


K


W dniu poniedzia=C5=82ek, 24 czerwca 2013 20:42:04 UTC+2 u=C5=BCytkownik Ge=
offrey=20
Romer napisa=C5=82:
>
> It's surprisingly hard to come up with a good, teachable set of rules for=
=20
> when to use std::move. For example, it would be nice to be able to say "y=
ou=20
> can leave out std::move when returning a named local variable", but this =
is=20
> not always the case. Consider:
>
> std::unique_ptr<const Foo> FooFactory() {
>   std::unique_ptr<Foo> result(new Foo);
>   // ...
>   return std::move(result);
> }
>
> The "std::move" is mandatory here, because an lvalue can only be=20
> implicitly treated as an rvalue during copy operations, and in "return=20
> result;", 'result' is not copied, but used as the input for an implicit=
=20
> conversion. It seems like this should be fixable, perhaps by expanding th=
e=20
> implicit-rvalue rules, but I'm not sure what all the consequences would b=
e=20
> (at a minimum, this could change the behavior of existing code that has=
=20
> separate rvalue and lvalue overloads for an implicit conversion operation=
).
>
> Conversely, it would be nice to be able to say "When in doubt, just use=
=20
> std::move if you want move semantics, because it never hurts", but=20
> std::move can in fact be a pessimization:
>
> std::array<Foo, 10000> MakeHugeArray() {
>   std::array<Foo, 10000> result;
>   // ...
>   return std::move(result);
> }
>
> As written, the return statement incurs 10,000 invocations of Foo's move=
=20
> constructor (or, worse, its copy constructor), but if std::move is=20
> eliminated, the return statement becomes effectively free, because it's a=
n=20
> elidable copy/move. In this case it's not at all clear to me how to fix t=
he=20
> problem; it seems like a fix would either require special-casing std::mov=
e=20
> (which seems like a hack, and breaks the convention that the core-languag=
e=20
> standard tries not to refer to the library standard), or require the=20
> implementation to 'see into' the implementation of a function called in a=
=20
> return statement (which seems impractical).=20
>
> I think it's worth trying to fix these issues, despite the difficulties,=
=20
> because being able to provide reliable "rules of thumb" would substantial=
ly=20
> improve the learning curve for std::move. Can anyone suggest how these=20
> issues could be fixed?
> =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_819_28922083.1403893330951
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br>I learnt that you should apply std::move only to the t=
ypes that hold resources (like heap memory) behind the scenes. That means y=
ou shouldn't apply std::move to fundamental types and types that are bitwis=
e copyable (with shallow copy operation like aggregates). In fact, this app=
lies to std::array as well, but is limited to the implementation of type st=
ored in such array. Typical implementation of std::array might be:<br><br><=
div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bo=
rder-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; wor=
d-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettypri=
nt"><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">template</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span styl=
e=3D"color: #008;" class=3D"styled-by-prettify">class</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> T</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> size_t n</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"><br>array </span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> T storage</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">[</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
>n</span><span style=3D"color: #660;" class=3D"styled-by-prettify">];</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #800;" class=3D"styled-by-prettify">/* some non-virual member=
 function here... */</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> &nbsp;</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span></div></code></div><br>What if T is not bitwise copyable or it is,=
 for instance std::vector&lt;U&gt; of thousands of U? Should I apply move o=
peration to every element of such std::array&lt;std::vector&lt;U&gt;, n&gt;=
?&nbsp; It depends mainly on N/RVO optimisations. To be strict, moving blin=
dly may not be a good idea.<br><br>Issuing a compiler warning on possible p=
essimization when applying std::move is reasonable. Implicit conversion are=
 evil in my opinion and such conversion that changes properties of a type (=
like CV-qualifiers) should result in warning. Compiler warnings are to help=
 developers.<br><br><br>K<br><br><br>W dniu poniedzia=C5=82ek, 24 czerwca 2=
013 20:42:04 UTC+2 u=C5=BCytkownik Geoffrey Romer napisa=C5=82:<blockquote =
class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1p=
x #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">It's surprisingly hard to=
 come up with a good, teachable set of rules for when to use std::move. For=
 example, it would be nice to be able to say "you can leave out std::move w=
hen returning a named local variable", but this is not always the case. Con=
sider:<div>
<br></div><div>std::unique_ptr&lt;const Foo&gt; FooFactory() {</div><div>&n=
bsp; std::unique_ptr&lt;Foo&gt; result(new Foo);</div><div>&nbsp; // ...</d=
iv><div>&nbsp; return std::move(result);</div><div>}</div><div><br></div><d=
iv>The "std::move" is mandatory here, because an lvalue can only be implici=
tly treated as an rvalue during copy operations, and in "return result;", '=
result' is not copied, but used as the input for an implicit conversion. It=
 seems like this should be fixable, perhaps by expanding the implicit-rvalu=
e rules, but I'm not sure what all the consequences would be (at a minimum,=
 this could change the behavior of existing code that has separate rvalue a=
nd lvalue overloads for an implicit conversion operation).</div>
<div><br></div><div>Conversely, it would be nice to be able to say "When in=
 doubt, just use std::move if you want move semantics, because it never hur=
ts", but std::move can in fact be a pessimization:</div><div>
<br></div><div>std::array&lt;Foo, 10000&gt; MakeHugeArray() {</div><div>&nb=
sp; std::array&lt;Foo, 10000&gt; result;</div><div>&nbsp; // ...</div><div>=
&nbsp; return std::move(result);</div><div>}</div><div><br></div><div>As wr=
itten, the return statement incurs 10,000 invocations of Foo's move constru=
ctor (or, worse, its copy constructor), but if std::move is eliminated, the=
 return statement becomes effectively free, because it's an elidable copy/m=
ove. In this case it's not at all clear to me how to fix the problem; it se=
ems like a fix would either require special-casing std::move (which seems l=
ike a hack, and breaks the convention that the core-language standard tries=
 not to refer to the library standard), or require the implementation to 's=
ee into' the implementation of a function called in a return statement (whi=
ch seems impractical).&nbsp;</div>
<div><br></div><div>I think it's worth trying to fix these issues, despite =
the difficulties, because being able to provide reliable "rules of thumb" w=
ould substantially improve the learning curve for std::move. Can anyone sug=
gest how these issues could be fixed?<br>
</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_819_28922083.1403893330951--

.
