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 9028321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:57:26+00:00 2026-06-16T06:57:26+00:00

Possible Duplicate: What happens if I return literal instead of declared std::string? Consider the

  • 0

Possible Duplicate:
What happens if I return literal instead of declared std::string?

Consider the following code

string getName () {
    return "meme";
}

string name = getName();

The function getName() returns a temporary object. In C++03, I understand the copy constructor of string gets called and the temporary object is destroyed. Actually it seems that the compiler (at least in GCC 4.7) optimizes line 5 by not creating the object name but replacing it with the temporary object itself and not destroying the temporary object. (I tried with a MyVector class, not std::string)

As defined in C++11 standards,

  1. Is getName() returning an rvalue?

  2. In line 5 above, which constructor of string gets called (move or copy) ? Should I necessarily call std::move() for the move constructor to get called?

  3. With move semantics, is it less efficient then the “copy elision” optimization provided by the compiler?

  • 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-16T06:57:27+00:00Added an answer on June 16, 2026 at 6:57 am
    1. Functions don’t return rvalues or lvalues. The value categories apply to expressions. So an expression that calls a function may be an rvalue or lvalue. In this case, the expression getName() is an rvalue expression because the function getName returns an object by value. This comes from §5.2.2/10:

      A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise.

      Your functions result type is not an lvalue or rvalue reference, so the function call is a prvalue. prvalue expressions are a subset of rvalue expressions.

    2. The move constructor will be used (unless it is elided, which it may be). That’s because getName() is an rvalue, so the constructor of std::string that takes an rvalue reference will better match the argument. Note that even if the move construction is elided, the move constructor must still be accessible. That is, the code must be compilable even if it is not elided.

    3. In general, the optimization of copy or move elision will completely get rid of any copying or moving. So of course it’s faster than actually doing a move. If a move is elided, literally nothing happens. There will be no code emitted for that move. The compiler achieves this by directly constructing the object in the location it would be copied or moved to.

    It’s worth mentioning that this could also be equivalently optimized:

    string getName () {
      std::string str("meme");
      return str;
    }
     
    string name = getName();
    

    Here, two moves will be elided (involving what is commonly known as Named Return Value Optimization). There are two points to consider here. First, return str; meets the criteria for copy/move elision (§12.8/31):

    This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

    • 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
    • …

    Second is that, although str is an lvalue, it will still be moved from because it fits a special case given by the standard (§12.8/32):

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Will code in finally run after a redirect? Hello, What happens when
Possible Duplicate: Where are literal strings placed and why can I return pointers to
Possible Duplicate: How to filter items from a std::map? What happens if you call
Possible Duplicate: What happens to a declared, uninitialized variable in C? Does it have
Possible Duplicate: In what order does evaluation of post-increment operator happen? Consider the following
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: Operator overloading I'm making a long awaited return to C++ and there's
Possible Duplicate: In C++, what is a virtual base class? In this code when
Possible Duplicate: Clang, std::shared_ptr and std::less/operator< So yeah, the title is pretty much the

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.