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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:07:52+00:00 2026-06-01T07:07:52+00:00

Following on from a comment I made on this: passing std::vector to constructor and

  • 0

Following on from a comment I made on this:

passing std::vector to constructor and move semantics
Is the std::move necessary in the following code, to ensure that the returned value is a xvalue?

std::vector<string> buildVector()
{
  std::vector<string> local;

  // .... build a vector

  return std::move(local);
}

It is my understanding that this is required. I have often seen this used when returning a std::unique_ptr from a function, however GManNickG made the following comment:

It is my understanding that in a return statement all local variables are automatically xvalues (expiring values) and will be moved, but I’m unsure if that only applies to the returned object itself. So OP should go ahead and put that in there until I’m more confident it shouldn’t have to be. 🙂

Can anyone clarify if the std::move is necessary?

Is the behaviour compiler dependent?

  • 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-01T07:07:54+00:00Added an answer on June 1, 2026 at 7:07 am

    You’re guaranteed that local will be returned as an rvalue in this situation. Usually compilers would perform return-value optimization though before this even becomes an issue, and you probably wouldn’t see any actual move at all, since the local object would be constructed directly at the call site.

    A relevant Note in 6.6.3 [“The return statement”] (2):

    A copy or move operation associated with a return statement may be elided or considered as an rvalue for the purpose of overload resolution in selecting a constructor (12.8).

    To clarify, this is to say that the returned object can be move-constructed from the local object (even though in practice RVO will skip this step entirely). The normative part of the standard is 12.8 [“Copying and moving class objects”] (31, 32), on copy elision and rvalues (thanks @Mankarse!).


    Here’s a silly example:

    #include <utility>
    
    struct Foo
    {
        Foo()            = default;
        Foo(Foo const &) = delete;
        Foo(Foo &&)      = default;
    };
    
    Foo f(Foo & x)
    {
        Foo y;
    
        // return x;         // error: use of deleted function ‘Foo::Foo(const Foo&)’
        return std::move(x); // OK
        return std::move(y); // OK
        return y;            // OK (!!)
    }
    

    Contrast this with returning an actual rvalue reference:

    Foo && g()
    {
        Foo y;
        // return y;         // error: cannot bind ‘Foo’ lvalue to ‘Foo&&’
        return std::move(y); // OK type-wise (but undefined behaviour, thanks @GMNG)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

G'day, Inspired by a comment I made in my answer to this question on
*NOTE - This code is from a third party extension. I had no part
I'm trying to extract the following items from a C file: Comments (single and
Following from my last question which @Jon Skeet gave me a lot of help
Following from these question Subset sum problem and Sum-subset with a fixed subset size
Is the following From header incorect? // To send HTML mail, the Content-type header
I'm getting the following from a third party library (one example): @%SystemRoot%\SomePath\SomeFile.Dll,-203 I know
Sometimes I get the following from SQL Server 2005 when executing a stored procedure:
I have a problem following from my previous problem . I also have the
In TestDriven.Net I can set the following from the TestDriven.Net Options Pane Run tests

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.