Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8248871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:20:38+00:00 2026-06-07T23:20:38+00:00

12.8 Copying and moving class objects [class.copy] §31 and §32 say: in a return

  • 0

12.8 Copying and moving class objects [class.copy] §31 and §32 say:

in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

When the criteria for elision of a copy operation are met or would be met save for the fact that the source object is a function parameter, and the object to be copied is designated by an lvalue, overload resolution to select the constructor for the copy is first performed as if the object were designated by an rvalue.

Hence we can write:

unique_ptr<int> make_answer()
{
    unique_ptr<int> result(new int(42));
    return result;   // lvalue is implicitly treated as rvalue
}

However, I noticed that g++ 4.6.3 also accepts lvalues that are not names, for example:

    return (result);
    return *&result;
    return true ? result : result;

By contrast, return rand() ? result : result; does not work. Is the compiler’s optimizer interfering with the language semantics? As I interpret the standard, return (result); should not compile, because (result) is not a name, but a parenthesized expression. Am I right or wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:20:39+00:00Added an answer on June 7, 2026 at 11:20 pm

    Regarding parenthesized expressions [√]

    You are wrong when talking about parenthesized expressions and that it shouldn’t be able to trigger a move when being returned and containing only the name of a moveable object.

    5.1.1/1      General      [expr.prim.general]

    A parenthesized expression is a primary expression whose type and
    value are identical to those of the enclosed expression. The
    presence of parentheses does not affect whether the expression is an lvalue. The parenthesized expression can be used in exactly the same contexts as those where the enclosed expression can be used, and with the same meaning, except as otherwise indicated.


    Regarding the constexpr conditional operator [╳]

    The way I interpret the standard in regards to constant-expressions and he coditional operator is that the use of return true ? result : result is well-behaved since it is a constant expression and therefore equivalent to return result;

    I have now gone through the standard more carefully and nowhere does it say that a constant conditional-expression is the same as if only the "returned" expression would have been written.

    true ? <expr1> : <expr2>; // this is not the same as just writing <expr1>;
    

    Regarding return *&result; [╳]

    In C99 it is explicitly stated that *&result is the exact equivalent of having written result instead, this is not the case in the C++ specification.

    Though we can all agree on that using *&result will indeed yield the same lvalue as result, but according to the standard *&result (of course) isn’t an expression where "the expression is the name of a non-volatile automatic object".

    Sure, the expression contains an appropriate name, but it’s not just only that.


    To sum things up…

    return result; // #1, OK
    

    return (result);                  // as described earlier, OK
    return true ? result : result;    // as described earlier, ill-formed
    return rand () ? result : result; // as described earlier, ill-formed
    return *&result;                  // as described earlier, ill-formed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering if in C++0x 12.8 Copying and Moving class objects [class.copy] paragraph
Similar to the way Resharper makes copying or moving a type to another file
Is there a standard Java library that handles common file operations such as moving/copying
I have a simple class that includes a GenericRelation class Item(models.Model): name = models.CharField
I have recently been copying and moving a large number of files (~400,000). I
I was reorganizing some projects by copying (not moving) some folders under version control
When copying (or moving) multiple files using drag-and-drop in Windows XP, the copying process
I am copying a file from a remote server using copy (and also tried
I am copying a resource into another folder before packaging using the maven-dependecy-plugins copy
This is so wrong. I want to perform a large copy operation; moving 250

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.