220 5197 <CAOfiQq==HeEUmRGhashVO2486N+XCkH25dJY9xxEPc87w0omBQ@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Richard Smith <richard@metafoo.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Teachability problems with std::move
Date: Mon, 24 Jun 2013 12:14:17 -0700
Lines: 77
Approved: news@gmane.org
Message-ID: <CAOfiQq==HeEUmRGhashVO2486N+XCkH25dJY9xxEPc87w0omBQ@mail.gmail.com>
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: text/plain; charset=ISO-8859-1
X-Trace: ger.gmane.org 1372101259 10329 80.91.229.3 (24 Jun 2013 19:14:19 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 24 Jun 2013 19:14:19 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDVNBJG4YAIBBCVVUKHAKGQE3GKUK7I@isocpp.org Mon Jun 24 21:14:20 2013
Return-path: <std-proposals+bncBDVNBJG4YAIBBCVVUKHAKGQE3GKUK7I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ye0-f198.google.com ([209.85.213.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDVNBJG4YAIBBCVVUKHAKGQE3GKUK7I@isocpp.org>)
	id 1UrCDT-0007UR-K9
	for gclcip-std-proposals@m.gmane.org; Mon, 24 Jun 2013 21:14:19 +0200
Original-Received: by mail-ye0-f198.google.com with SMTP id m13sf819786yen.5
        for <gclcip-std-proposals@m.gmane.org>; Mon, 24 Jun 2013 12:14:18 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=x-beenthere:mime-version:sender:in-reply-to:references:date
         :message-id:subject:from:to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-google-group-id:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=kAnYfxt4wjd5lbBLMSY7gewOsECiVj9knHbYLxGBFWU=;
        b=n8o/rJALiOO/SX6P9YBLDetEPMj8Hmjyq3MeA7PwYhOqQe+hNfSLrdljGWW+2W+FXU
         a5fEnu2QqCVOuAguFwl7+TzFkNxiJVgvu2AUc1vAI07H+LrH6POIFeSvjFHooardvc2W
         ht9wx69OECBJy7REO9hsFRxu2SaYXODAs8BlplK2Fw98cRxI4q9KQ+Ve2fuXZEEI/E0P
         s7zzxzh7tMXLJQpAN4/YNxkGFrjLP3eRyNThwtGJzgBzk+XLHRpC0XJ1fdW4VwX0xsrr
         rAARwvFEGEuJHChZyeq48nwWD3JGq5gWbs+dD7lhMuWd9axtlexNZ8qzZJlbi3WmGG5v
         kRhQ==
X-Received: by 10.236.124.165 with SMTP id x25mr14295740yhh.4.1372101258764;
        Mon, 24 Jun 2013 12:14:18 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.117.231 with SMTP id kh7ls2075925qeb.26.gmail; Mon, 24 Jun
 2013 12:14:17 -0700 (PDT)
X-Received: by 10.52.27.172 with SMTP id u12mr9713855vdg.64.1372101257922;
        Mon, 24 Jun 2013 12:14:17 -0700 (PDT)
Original-Received: from mail-ve0-x22c.google.com (mail-ve0-x22c.google.com [2607:f8b0:400c:c01::22c])
        by mx.google.com with ESMTPS id ya5si5258156vec.95.2013.06.24.12.14.17
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Mon, 24 Jun 2013 12:14:17 -0700 (PDT)
Received-SPF: pass (google.com: domain of metafoo@gmail.com designates 2607:f8b0:400c:c01::22c as permitted sender) client-ip=2607:f8b0:400c:c01::22c;
Original-Received: by mail-ve0-f172.google.com with SMTP id jz10so9261888veb.3
        for <std-proposals@isocpp.org>; Mon, 24 Jun 2013 12:14:17 -0700 (PDT)
X-Received: by 10.52.70.137 with SMTP id m9mr10331360vdu.30.1372101257564;
 Mon, 24 Jun 2013 12:14:17 -0700 (PDT)
Original-Sender: metafoo@gmail.com
Original-Received: by 10.58.125.73 with HTTP; Mon, 24 Jun 2013 12:14:17 -0700 (PDT)
In-Reply-To: <CA+cyFgsYFbjG1M98PJS-XVzuJYbQmt7=4GOqS5_V5-WVJ0caLg@mail.gmail.com>
X-Original-Sender: richard@metafoo.co.uk
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of metafoo@gmail.com designates 2607:f8b0:400c:c01::22c as permitted
 sender) smtp.mail=metafoo@gmail.com;       dkim=pass header.i=@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:5197
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/5197>

On Mon, Jun 24, 2013 at 11: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 "you
> can leave out std::move when returning a named local variable", but this is
> 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 implicitly
> 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-rvalue
> 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 and
> lvalue overloads for an implicit conversion operation).

Yes, for NRVO, I think the "same cv-unqualified type as the function
return type" restriction only needs to apply to copy-elision and not
to implicit move. That is, I think we should treat 'result' as an
xvalue here:

T f() {
  U result;
  return result;
}

.... independent of the types of T and U, but we should only be
permitted to perform copy-elision of they're the same cv-unqualified
type. Most generalizations of the implicit-rvalue rules are fraught
with peril, but this one seems relatively safe.

> 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);
> }

I suggest you talk to your compiler vendor (Hi!) and ask for a warning
for this case.

> 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 an
> elidable copy/move. 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 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 'see into' the implementation of a function called in a
> return statement (which seems impractical).
>
> I think it's worth trying to fix these issues, despite the difficulties,
> because being able to provide reliable "rules of thumb" would substantially
> improve the learning curve for std::move. Can anyone suggest how these
> issues could be fixed?

Per the above, I think we can make "you can leave out std::move when
returning a named local variable" be the right advice, with the added
bonus of a compiler warning if you put in a pessimizing std::move.

-- 

--- 
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/.



.
