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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:05:15+00:00 2026-05-24T02:05:15+00:00

There are many questions on this site about return value optimization (I suppose it’s

  • 0

There are many questions on this site about return value optimization (I suppose it’s a fairly confusing topic), but I can’t seem to find one that answers my particular question. If I have code that looks like this:

returnType function() { stuff....}

void someOtherFunction()
{
    returnType var = function();
    more stuff...
}

I am told that the compiler may decide to not use two instances of returnType in someOtherFunction(). Logically, I would expect that function() would generate an object of type returnType, and someOtherFunction() would receive that value via copy constructor (overloaded or not) into a temporary value. I would then expect that temporary value to be copied via assignment (which could be overloaded and in theory could have any kind of functionality!) into var, which would have previously been initialized via the default constructor.

I see a potential issue here. What happens if there is not this temporary copy of returnType in someOtherFunction()? Wouldn’t var have to be filled, via copy constructor, with the returned value from function() directly? If so, wouldn’t the assignment operator never be called? If so, and if the assignment operator were overloaded, couldn’t that change the functionality of the program? If so, does that mean that it’s the programmer’s responsibility to ensure that = always does the same thing as a copy constructor? And I hate to run this long chain of questions, but if so, why does C++ allow you to define copy constructors to do something other than assignment?

  • 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-24T02:05:16+00:00Added an answer on May 24, 2026 at 2:05 am

    I would then expect that temporary value to be copied via assignment
    (which could be overloaded and in theory could have any kind of
    functionality!) into var, which would have previously been initialized
    via the default constructor.

    Well, for starters, that’s quite wrong.

    int x = 0;
    int x(0);
    

    Those two lines are the same thing- a constructor call. There are some differences- the first cannot call explicit constructors, I believe- but they are the same function call. There is no default construction and no assignment operator call. They are both direct constructions.

    Basically, the Standard says “If you do something other than copying an object in the copy constructor, it’s your own dumb fault and I laugh as your program doesn’t exhibit the expected behaviour when the optimizer eliminates the calls”. Those are my own paraphrased words, of course, but the Standard is very explicit on the optimizer being allowed to eliminate copies. In C++0x then this applies to moves too.

    Your code fragment above is really

    returnType function() { stuff....}
    
    void someOtherFunction()
    {
        returnType var(function());
        more stuff...
    }
    

    That isn’t the optimized version, that’s what it really is. The assignment operator is never called. And with NRVO, it looks something like

    void function(void* mem) { // construct return value into mem
        new (mem) returnType;
        // Do shiz with returnType;
    }
    void someOtherFunction() {
        // This doesn't respect some other things like alignment
        // but it's the basic idea
        char someMemory[sizeof(returnType)];
        function(someMemory);
        // more stuff here
    }
    

    Of course, the compiler also has to deal with destructing the object even in the case of an exception, and making sure all aliases are of the correct type, and alignment, and some other things I didn’t deal with in my sample, but hopefully you get the general gist.

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

Sidebar

Related Questions

There seem to be many questions asked about this subject here on stackoverflow, but
NOTE: I know there are many questions that talked about that but I'm still
Many people have asked about Rails hosting on this site, but I'm not familiar
I know, there are many different questions and so many answers about this problem...
There seem to be many many unanswered questions about this. Hopefully, I'll get lucky
First, I noticed there are many questions regarding this, lots marked as duplicate. I
There are many similar questions, but apparently no perfect match, that's why I'm asking.
I know there are already many questions like mine but I found no answer
There are a few questions that approach an answer to this question, but none
I have read a number of questions on this site about this issue, 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.