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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:10:15+00:00 2026-05-26T08:10:15+00:00

I have been learning about move constructors over the last day or so, trying

  • 0

I have been learning about move constructors over the last day or so, trying to stick to a general rule of returning by value as most people seem to suggest, and have come across an interesting (to me) dilemma.

Assume that I have an expensive to construct/copy class ‘C’ that has correctly defined copy constructor, assignment operator, move constructor and move assignment operator.

First, this piece of code elides the copy constructor as I expected:

C make_c1() {
    return C();
}

as does this:

C make_c2() {
    C tmp;
    return tmp;
}

and so does this (whether I pass in a 1 or 2):

C make_c3(int a) {
    return a == 1 ? make_c1() : make_c2();
}

It’s when I get to this that I have an issue:

C make_c4(int a) {
    C tmp;
    return a == 1 ? make_c1() : tmp;
}

Passing in a 1 triggers RVO for the result of make_c1, but passing in a 2 triggers the copy constructor on tmp.

Amending the function to the following causes the move constructor to be triggered for tmp instead:

C make_c5(int a) {
    C tmp;
    return a == 1 ? make_c1() : std::move(tmp);
}

All great and wonderful except…

In these simple examples, RVO has been triggered pretty much as I’d hoped.

However what if my code is slightly more complex and on some compilers doesn’t evoke RVO in that last function? In that case, I’d need to wrap my call to make_c1 in std::move, which will make the code less efficient on those compilers that do evoke RVO.

So my questions are:

  1. Why was the move constructor not invoked in make_c4 when I returned my local object? (It’s about to be destroyed after all).
  2. In the function make_c5, should I return the results of make_c1 by value or by moving it? (To avoid different versions of the code for differing compilers/platforms).
  3. Is there a better way to code the final function so that it does the right thing for a reasonable compiler implementation?

The compiler I have been playing with is GCC 4.5.3 on Cygwin.

  • 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-26T08:10:16+00:00Added an answer on May 26, 2026 at 8:10 am

    The implicit move-on-return is only legal in the same contexts in which RVO is legal. And RVO is legal 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 ([class.copy]/p31/b1).

    If you transform make_c4 to:

    C make_c4(int a) {
        C tmp;
        if (a == 1)
            return make_c1();
        return tmp;
    }
    

    Then you get the expected move construction for the call to make_c4(2). Your make_c5 rewrite is not desirable for exactly the reasons you state.

    Update:

    I should have also included a reference to [expr.cond]/p6/b1 which explains the semantics of the conditional expression when the second expression is a prvalue and the third is an lvalue, but both have the same type:

    The second and third operands have the same type; the result is of
    that type. If the operands have class type, the result is a prvalue
    temporary of the result type, which is copy-initialized from either
    the second operand or the third operand depending on the value of the
    first operand.

    I.e. this paragraph specifies that the resultant prvalue of the conditional is copy-initialized, from the 3rd argument in your example. Copy-initialization is defined in [dcl.init]/p14. When the source of a copy-initialization is a class-type lvalue, this will invoke the type’s copy constructor. If the source is an rvalue, it will invoke the move constructor if one exists, else it will invoke the copy constructor.

    The specification of the conditional expression has no allowance for an implicit move from an lvalue argument, even if the conditional expression is part of a return expression. It is possible that the language could have been crafted to allow such an implicit move, but as far as I know, it was never proposed. Furthermore the existing specification of the conditional expression is already extremely complicated, making such change to the language all the more difficult.

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

Sidebar

Related Questions

As of last night, I decided to start learning about WPF and have been
I'm learning Python and have been trying to understand more about the details of
I have been learning about the basics of C# but haven't come across a
Recently I have been learning about WMI and WQL. I found out the list
I am currently learning about basic networking in java. I have been playing around
I've been learning wpf for about a week now.. and i have a basic
I have been learning about various functional languages for some time now including Haskell,
I have been learning about TDD (using JUnit) and I have a doubt about
I have recently been learning more about design patterns and thought I'd take a
I'm 14 and have been learning java for about 4/5 months. I'm coding a

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.