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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:09:27+00:00 2026-06-09T20:09:27+00:00

Possible Duplicate: C++11 rvalues and move semantics confusion What I think is correct is

  • 0

Possible Duplicate:
C++11 rvalues and move semantics confusion

What I think is correct is

std::string GetLine()
{
    std::string str;
    std::getline(std::cin, str);
    return std::move(str);
}

But at this link http://www.cprogramming.com/c++11/rvalue-references-and-move-semantics-in-c++11.html
( check the header part Returning an explicit rvalue-reference from a function)

which is #1 google search hit for move semantics shows a similar function signature as

int&& GetInt()
{
    int x = 0;
    // code here
    return std::move(x);
}

From what I read at other places && means rvalue reference so in this case its returning a reference to an object that doesn’t exist.

So which is it?

(Yes I know moving an int has no real benifit but the question is whether to use the return type of std::string or std::string&& in the first function. And if that is how it should be done for all types.)

  • 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-09T20:09:29+00:00Added an answer on June 9, 2026 at 8:09 pm

    You are absolutely correct that the int&& GetInt() example is wrong, and is returning a reference to an object that is destroyed. However, unless I missed it, the link you posted does not actually show any code returning a reference to a local variable. Instead I see a reference to a global variable being returned, which is okay.

    Here is how you use move semantics when returning:

    std::string func()
    {
        std::string rv;
        /* ... */
        return rv;
    }
    

    You generally should not use std::move() when returning an object. The reason for this is that moving is already implicitly allowed anytime RVO could occur, and using std::move() will suppress RVO. So using std::move() will never be better and will often be worse than just returning normally.


    Again, using std::move() can be worse than simply naming the variable to be returned because it suppresses the return value optimization. The return value optimization allows for an object to be returned to the caller without needing to copy that object

    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

    — [class.copy] 12.8/31

    But using std::move() prevents the return expression from being the name of the object you’re returning. Instead the expression is more complicated and the language is no longer allowed to give it special handling.

    The reason just naming the object is not worse than using std::move() is because there’s another rule that says an expression can already be treated as an rvalue without needing std::move().

    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: std::string and its automatic memory resizing I am just curious, how are
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Why is String.Concat not optimized to StringBuilder.Append? One day I was ranting
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: git - removing a file from source control (but not from the
Possible Duplicate: Parse C# string to DateTime I am facing a problem while converting
Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1
Possible Duplicate: Parsing XML String in C# How to process the below XML file
Possible Duplicate: Find the “#” character in a string with Javascript How do I

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.