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

  • Home
  • SEARCH
  • 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 6086163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:43:15+00:00 2026-05-23T11:43:15+00:00

Consider the case when whole objects with move semantics enabled are returned from functions,

  • 0

Consider the case when “whole” objects with move semantics enabled are returned from functions, as with std::basic_string<>:

std::wstring build_report() const
{
    std::wstring report;
    ...

    return report;
}

Can I then realistically be expected to make the “best” choice whether to use the returned string with move semantics, as in

const std::wstring report(std::move(build_report()));

or if I should rely on (N)RVO to take place with

const std::wstring report(build_report());

or even bind a const reference to the temporary with

const std::wstring& report(build_report());

What scheme is there to make a deterministic choice of these options, if any?

EDIT 1: Note that the usage of std::wstring above is just an example of a move semantics enabled type. It just as well be swapped for your arbitrary_large_structure. 🙂

EDIT 2: I checked the generated assembly when running a speed-optmized release build in VS 2010 of the following:

std::wstring build_report(const std::wstring& title, const std::wstring& content)
{
    std::wstring report;
    report.append(title);
    report.append(content);

    return report;
}

const std::wstring title1(L"title1");
const std::wstring content1(L"content1");

const std::wstring title2(L"title2");
const std::wstring content2(L"content2");

const std::wstring title3(L"title3");
const std::wstring content3(L"content3");

int _tmain(int argc, _TCHAR* argv[])
{
    const std::wstring  report1(std::move(build_report(title1, content1)));
    const std::wstring  report2(build_report(title2, content2));
    const std::wstring& report3(build_report(title3, content3));

    ...

    return 0;
}

The 2 most interesting outcomes:

  • Explicitly calling std::move for report1 to use the move constructor triples the instruction count.
  • As noted by James McNellis in his answer below, report2 and report3 does indeed generate identical assembly with 3 times fewer instructions than explicitly calling std::move.
  • 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-05-23T11:43:15+00:00Added an answer on May 23, 2026 at 11:43 am

    std::move(build_report()) is wholly unnecessary: build_report() is already an rvalue expression (it is a call of a function that returns an object by value), so the std::wstring move constructor will be used if it has one (it does).

    Plus, when you return a local variable, it gets moved if it is of a type that has a move constructor, so no copies will be made, period.

    There shouldn’t be any functional difference between declaring report as an object or as a const-reference; in both cases you end up with an object (either the named report object or an unnamed object to which the report reference can be bound).

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

Sidebar

Related Questions

Consider this case: dll = LoadDLL() dll->do() ... void do() { char *a =
Consider this case: public Class1 { public static final String ONE = ABC; public
Consider the case of blogspot.com the domain is the one blogspot.com only but suppose
EDIT: I missed a crucial point: .NET 2.0 Consider the case where I have
I'm using LINQ to query data. Consider a case where the user only wants
Consider the following case: public class A { public A() { b = new
Question is should i use properties for my view controllers? Consider the following case:
Consider the following use case 1. User selects a product to purchase on seller's
Please consider the following simple use case: public class Foo { public virtual int
consider this piece of scala swing code detail.reactions += { case ButtonClicked(but) => detail.contents

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.