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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:27:50+00:00 2026-06-02T18:27:50+00:00

Consider the following: ComplexObject foo() { ComplexObject temp; //Do things with temp ComplexObject result(temp,

  • 0

Consider the following:

ComplexObject foo()
{
    ComplexObject temp;
    //Do things with temp
    ComplexObject result(temp, SOME_OTHER_SETTING); //1
    //Do things with result. Do not use temp at all
    return result; //2
}

ComplexObject foo()
{
    ComplexObject temp;
    //Do things with temp
    ComplexObject result(std::move(temp), SOME_OTHER_SETTING); //1
    //Do things with result. Do not use temp at all
    return std::move(result); //2
}

with the assumption that ComplexObject has a move constructor which is far more efficient than it’s copy constructor.

Is the compiler allowed to effectively transform the first code into the second code, because it knows that ComplexObject cannot be used for the remainder of that block?

  • 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-02T18:27:53+00:00Added an answer on June 2, 2026 at 6:27 pm

    Not for temp, but the compiler can perform other optimizations under the as-if rule, which may have the same effect.

    For result, there is a special rule regarding return statements, that will use a move if possible (and elision is preferred over either move or copy).

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

Sidebar

Related Questions

Consider following source files 1.cpp #include <iostream> using namespace std; struct X { X()
Please consider following schema CREATE table articles ( id Int UNSIGNED NOT NULL AUTO_INCREMENT,
Consider the following example: HTML: <div id=cool-div></div> JS/jQuery: var ComplexObject = function() { this.Append
Consider following statement: C a, b; //C contains c1, c2 and c3 all integers
Consider following code public class City { public string Name { get { return
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
Consider following simple example: #!perl -w use strict; sub max { my ($a, $b)
please consider following code #include <iostream> using namespace std; class Digit { private: int
Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Consider following DOM fragment: <div id=div-1>foo</div> <div id=div-2>bar</div> Is it possible to insert HTML

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.