From -8992535245583918943
X-Google-Thread: 7894ca11fe,a4bc43b401c1097f,start
X-Google-Attributes: gid7894ca11fe,public,usenet
X-Google-NewGroupId: yes
X-Google-Language: ENGLISH,ASCII-7-bit
Path: g2news2.google.com!news2.google.com!newsfeed.stanford.edu!news.kjsl.com!news.alt.net!frodo.cs.rpi.edu!not-for-mail
From: SG <s.gesemann@gmail.com>
Newsgroups: comp.std.c++
Subject: Is copy elision allowed for returning a function's parameter?
Date: Tue, 30 Jun 2009 09:28:38 CST
Organization: http://groups.google.com
Lines: 44
Sender: cppmods@cs.rpi.edu
Approved: james.dennett@gmail.com
Message-ID: <bf120619-cba9-4ff8-8ed8-5a9b79c0035c@k8g2000yqn.googlegroups.com>
NNTP-Posting-Host: netlab.cs.rpi.edu
Content-Type: text/plain; charset=ISO-8859-1
To: (Usenet)
Return-Path: <cppmods@ruralroute.cs.rpi.edu>
X-Original-Date: Mon, 29 Jun 2009 10:14:21 -0700 (PDT)
X-Submission-Address: std-c++@netlab.cs.rpi.edu
Xref: g2news2.google.com comp.std.c++:958

Hi!

Is copy elision allowed for returning a function's parameter?

In other words:

  string flip(string x)
  {
     reverse( x.begin(), x.end() );
     return x; // <-- Do I need std::move() here?
  }

In N2914.pdf we can read in section 12.8/15 [class.copy] that the
elision of a copy is permitted under some circumstances including:

 - in a return statement in a function with a class return type,
   when the expression is the name of a non-volatile automatic
   object with the same cv-qualified type as the function return
   type, the copy operation can be omitted by constructing the
   automatic object directly into the function's return value

and later we can read that, if a copy elision is allowed but cannot be
performed the compiler has to treat the source object as an rvalue
when selecting a constructor for the return value. My question is
whether a function's parameter which has been accepted by value can
qualify as a "non-volatile automatic object". From what I know current
C++ implementations don't support this kind of copy elision but that
doesn't mean that it is not allowed.

In case this elision is currently deemed illegal I have to ask why
this is and whether I'm missing an obvious problem with letting the
compiler treat such parameters as rvalues in a return expression.

[N2914] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2914.pdf

Cheers!
SG

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]



