220 11588 <CA+cyFgtNWh2sVd7ajaf+HwfLM2BiYGWjXEUVhOp84kpoVfrDkQ@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: "'Geoffrey Romer' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Teachability problems with std::move
Date: Fri, 27 Jun 2014 12:43:32 -0700
Lines: 257
Approved: news@gmane.org
Message-ID: <CA+cyFgtNWh2sVd7ajaf+HwfLM2BiYGWjXEUVhOp84kpoVfrDkQ@mail.gmail.com>
References: <CA+cyFgsYFbjG1M98PJS-XVzuJYbQmt7=4GOqS5_V5-WVJ0caLg@mail.gmail.com>
	<FC52D786-962E-4525-8997-E5C764AE5780@gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a11c295723d2ba204fcd686ae
X-Trace: ger.gmane.org 1403898223 15909 80.91.229.3 (27 Jun 2014 19:43:43 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 27 Jun 2014 19:43:43 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBD74DCN5SYHBBZESW6OQKGQEGIUBZQQ@isocpp.org Fri Jun 27 21:43:37 2014
Return-path: <std-proposals+bncBD74DCN5SYHBBZESW6OQKGQEGIUBZQQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ie0-f199.google.com ([209.85.223.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBD74DCN5SYHBBZESW6OQKGQEGIUBZQQ@isocpp.org>)
	id 1X0c3a-0005mQ-2G
	for gclcip-std-proposals@m.gmane.org; Fri, 27 Jun 2014 21:43:34 +0200
Original-Received: by mail-ie0-f199.google.com with SMTP id rd18sf31723220iec.2
        for <gclcip-std-proposals@m.gmane.org>; Fri, 27 Jun 2014 12:43:33 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=nkW9nMpXlRu1Yx5w89p0exr2mMtINmGsnCuyIPWiOsc=;
        b=EQ2T6tSZXxiaMEwP6zk+Un5z22+9a00bflCd7aq1Ryfx3HhUx3Tzl+qh+blGQOyCN0
         uA+gZmmp59LQsVHFhnJHNts5yH/zJhZbDPyaGYAMh3TMORWWXiufxbpoCmyYf9liTtJ/
         PzsdWr+A2WrMynonNP696bXqTNM+5SuVEVkcybAsTvSIGRZAm/C+R20Iiuo59zFIKY7G
         PRGGwwdFzeD+7oyKD3JRCDzOziNKU0mjAowGuRu/K0XAutEFPufUDJEQW79JCKu6myk7
         p5oxy5uTxlZ3RAVfIFUK9E8Y14nE6m0W1PfAuaoVSe+jeEOuSSUT+TJBpnOGyHkh5ulO
         zQSA==
X-Gm-Message-State: ALoCoQkff5rYw36UZqTTjWKMlMoqj9/SX41JPHkAWs5VWqFiorphmKDX8HxB/sth/2bl/dLUZlde
X-Received: by 10.182.148.1 with SMTP id to1mr12441477obb.50.1403898213181;
        Fri, 27 Jun 2014 12:43:33 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.84.72 with SMTP id k66ls769009qgd.96.gmail; Fri, 27 Jun
 2014 12:43:32 -0700 (PDT)
X-Received: by 10.140.22.134 with SMTP id 6mr34066710qgn.4.1403898212437;
        Fri, 27 Jun 2014 12:43:32 -0700 (PDT)
Original-Received: from mail-qg0-x22e.google.com (mail-qg0-x22e.google.com [2607:f8b0:400d:c04::22e])
        by mx.google.com with ESMTPS id k6si15112152qct.2.2014.06.27.12.43.32
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 27 Jun 2014 12:43:32 -0700 (PDT)
Received-SPF: pass (google.com: domain of gromer@google.com designates 2607:f8b0:400d:c04::22e as permitted sender) client-ip=2607:f8b0:400d:c04::22e;
Original-Received: by mail-qg0-f46.google.com with SMTP id q107so4702057qgd.5
        for <std-proposals@isocpp.org>; Fri, 27 Jun 2014 12:43:32 -0700 (PDT)
X-Received: by 10.224.131.74 with SMTP id w10mr37296831qas.100.1403898212176;
 Fri, 27 Jun 2014 12:43:32 -0700 (PDT)
Original-Received: by 10.96.223.131 with HTTP; Fri, 27 Jun 2014 12:43:32 -0700 (PDT)
In-Reply-To: <FC52D786-962E-4525-8997-E5C764AE5780@gmail.com>
X-Original-Sender: gromer@google.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of gromer@google.com designates 2607:f8b0:400d:c04::22e as permitted
 sender) smtp.mail=gromer@google.com;       dkim=pass header.i=@google.com;
       dmarc=pass (p=REJECT dis=NONE) header.from=google.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>
X-Original-From: Geoffrey Romer <gromer@google.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:11588
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/11588>

--001a11c295723d2ba204fcd686ae
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Thu, Jun 26, 2014 at 6:17 PM, David Krauss <potswa@gmail.com> wrote:

>
> On 2013=E2=80=9306=E2=80=9325, at 2:42 AM, Geoffrey Romer <gromer@google.=
com> wrote:
>
> 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 "y=
ou
> can leave out std::move when returning a named local variable", but this =
is
> not always the case.
>
>
> That doesn=E2=80=99t sound like something that should be taught. Students
> shouldn=E2=80=99t have the impression that they should avoid move, or omi=
t it just
> because they can.
>

Really? You wouldn't even teach students to omit move in an expression like
this?

Sink(std::move(Source()));


> 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 hurts", but
> 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
> constructor (or, worse, its copy constructor), but if std::move is
> eliminated, the return statement becomes effectively free, because it's a=
n
> elidable copy/move.
>
>
> I'm pretty sure there=E2=80=99s a DR for this. First-draft code doesn=E2=
=80=99t need to
> run full speed anyway. Students should learn general techniques for findi=
ng
> bottlenecks, not every performance quirk and pitfall.
>

The best way to avoid teaching performance quirks and pitfalls is to
eliminate them from the language. Failing that, students will have to learn
them eventually, although you can haggle over when.


> When fixing this code, it would be a good idea to add a comment that the
> result is expected to be constructed in-place, hence neither moved nor
> copied.
>
> By the way, this code is still giving you exactly what you asked for.
> Asking for a move when you actually want copy elision is, though
> reasonable, a confusion of two completely different things.
>

Can you give an example of when a programmer would not want a copy elision?


>
> In this case it's not at all clear to me how to fix the problem; it seems
> like a fix would either require special-casing std::move (which seems lik=
e
> a hack, and breaks the convention that the core-language standard tries n=
ot
> to refer to the library standard), or require the implementation to 'see
> into' the implementation of a function called in a return statement (whic=
h
> seems impractical).
>
>
> It may be doable for inline functions. Perhaps some future language
> iteration will see through the thicket.
>
> I think it's worth trying to fix these issues, despite the difficulties,
> because being able to provide reliable "rules of thumb" would substantial=
ly
> improve the learning curve for std::move. Can anyone suggest how these
> issues could be fixed?
>
>
> Say move() when you want a move.
>

That's just begging the question; I'm trying to teach people how to know
when they should want a move


> Debug the rest, and be aware that moves still carry a cost which may be a=
s
> much as a copy.
>

Would you say, then, that one should never apply std::move to a copyable
type?


>
>  --
>
> ---
> 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/.
>

--=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/.

--001a11c295723d2ba204fcd686ae
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">=
On Thu, Jun 26, 2014 at 6:17 PM, David Krauss <span dir=3D"ltr">&lt;<a href=
=3D"mailto:potswa@gmail.com" target=3D"_blank">potswa@gmail.com</a>&gt;</sp=
an> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div style=3D"word-wrap:break-word"><br><div=
><div class=3D""><div>On 2013=E2=80=9306=E2=80=9325, at 2:42 AM, Geoffrey R=
omer &lt;<a href=3D"mailto:gromer@google.com" target=3D"_blank">gromer@goog=
le.com</a>&gt; wrote:</div>
<br><blockquote type=3D"cite"><div dir=3D"ltr">It&#39;s surprisingly hard t=
o come up with a good, teachable set of rules for when to use std::move. Fo=
r example, it would be nice to be able to say &quot;you can leave out std::=
move when returning a named local variable&quot;, but this is not always th=
e case. </div>
</blockquote><div><br></div></div><div>That doesn=E2=80=99t sound like some=
thing that should be taught. Students shouldn=E2=80=99t have the impression=
 that they should avoid move, or omit it just because they can.</div></div>=
</div></blockquote>
<div><br></div><div>Really? You wouldn&#39;t even teach students to omit mo=
ve in an expression like this?</div><div><br></div><div>Sink(std::move(Sour=
ce()));</div><div><br></div><blockquote class=3D"gmail_quote" style=3D"marg=
in:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style=3D"word-wrap:break-word"><div><div class=3D""><br><blockquote ty=
pe=3D"cite"><div dir=3D"ltr"><div>Conversely, it would be nice to be able t=
o say &quot;When in doubt, just use std::move if you want move semantics, b=
ecause it never hurts&quot;, but std::move can in fact be a pessimization:<=
/div>
<div>
<br></div><div>std::array&lt;Foo, 10000&gt; MakeHugeArray() {</div><div>=C2=
=A0 std::array&lt;Foo, 10000&gt; result;</div><div>=C2=A0 // ...</div><div>=
=C2=A0 return std::move(result);</div><div>}</div><div><br></div><div>As wr=
itten, the return statement incurs 10,000 invocations of Foo&#39;s move con=
structor (or, worse, its copy constructor), but if std::move is eliminated,=
 the return statement becomes effectively free, because it&#39;s an elidabl=
e copy/move. </div>
</div></blockquote><div><br></div></div><div>I&#39;m pretty sure there=E2=
=80=99s a DR for this. First-draft code doesn=E2=80=99t need to run full sp=
eed anyway. Students should learn general techniques for finding bottleneck=
s, not every performance quirk and pitfall.</div>
</div></div></blockquote><div><br></div><div>The best way to avoid teaching=
 performance quirks and pitfalls is to eliminate them from the language. Fa=
iling that, students will have to learn them eventually, although you can h=
aggle over when.</div>
<div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8=
ex;border-left:1px #ccc solid;padding-left:1ex"><div style=3D"word-wrap:bre=
ak-word"><div><div> When fixing this code, it would be a good idea to add a=
 comment that the result is expected to be constructed in-place, hence neit=
her moved nor copied.</div>
<div><br></div><div>By the way, this code is still giving you exactly what =
you asked for. Asking for a move when you actually want copy elision is, th=
ough reasonable, a confusion of two completely different things.</div></div=
>
</div></blockquote><div><br></div><div>Can you give an example of when a pr=
ogrammer would not want a copy elision?</div><div>=C2=A0</div><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;=
padding-left:1ex">
<div style=3D"word-wrap:break-word"><div><div class=3D""><br><blockquote ty=
pe=3D"cite"><div dir=3D"ltr"><div>In this case it&#39;s not at all clear to=
 me how to fix the problem; it seems like a fix would either require specia=
l-casing std::move (which seems like a hack, and breaks the convention that=
 the core-language standard tries not to refer to the library standard), or=
 require the implementation to &#39;see into&#39; the implementation of a f=
unction called in a return statement (which seems impractical).=C2=A0</div>
</div></blockquote><div><br></div></div><div>It may be doable for inline fu=
nctions. Perhaps some future language iteration will see through the thicke=
t.</div><div class=3D""><br><blockquote type=3D"cite"><div dir=3D"ltr">
<div>I think it&#39;s worth trying to fix these issues, despite the difficu=
lties, because being able to provide reliable &quot;rules of thumb&quot; wo=
uld substantially improve the learning curve for std::move. Can anyone sugg=
est how these issues could be fixed?</div>
</div></blockquote><br></div></div><div>Say <font face=3D"Courier">move()</=
font> when you want a move. </div></div></blockquote><div><br></div><div>Th=
at&#39;s just begging the question; I&#39;m trying to teach people how to k=
now when they should want a move</div>
<div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8=
ex;border-left:1px #ccc solid;padding-left:1ex"><div style=3D"word-wrap:bre=
ak-word"><div>Debug the rest, and be aware that moves still carry a cost wh=
ich may be as much as a copy.=C2=A0</div>
</div></blockquote><div><br></div><div>Would you say, then, that one should=
 never apply std::move to a copyable type?</div><div>=C2=A0</div><blockquot=
e class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc sol=
id;padding-left:1ex">
<div style=3D"word-wrap:break-word"><div><br></div></div><div class=3D"HOEn=
Zb"><div class=3D"h5">

<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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><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 />

--001a11c295723d2ba204fcd686ae--

.
