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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:15:22+00:00 2026-05-29T10:15:22+00:00

In C++11 we can define copy and move constructors, but are both allowed on

  • 0

In C++11 we can define copy and move constructors, but are both allowed on the same class? If so, how do you disambiguate their usage? For example:

Foo MoveAFoo() {
  Foo f;
  return f;
}

Is the above a copy? A move? How do I know?

  • 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-29T10:15:23+00:00Added an answer on May 29, 2026 at 10:15 am

    Usually it will be neither due to RVO.

    If that optimisation can’t be performed, then it will be a move, because the object being returned is going out of scope (and will be destroyed just after). If it can’t be moved, then it will be copied. If it can’t be copied, it won’t compile.

    The whole point of move constructors is that when a copy is going to be made of an object that is just about to be destroyed, it is often unnecessary to make a whole copy, and the resources can be moved from the dying object to the object being created instead.

    You can tell when either the copy or move constructor is going to be called based on what is about to happen to the object being moved/copied. Is it about to go out of scope and be destructed? If so, the move constructor will be called. If not, the copy constructor.

    Naturally, this means you may have both a move constructor and copy constructor in the same class. You can also have a copy assignment operator and a move assignment operator as well.

    Update: It may be unclear as to exactly when the move constructor/assignment operator is called versus the plain copy constructor/assignment operator. If I understand correctly, the move constructor is called if an object is initialised with an xvalue (eXpiring value). §3.10.1 of the standard says

    An xvalue (an “eXpiring” value) also refers to an object, usually near
    the end of its lifetime (so that its resources may be moved, for
    example). An xvalue is the result of certain kinds of expressions
    involving rvalue references (8.3.2). [ Example: The result of calling
    a function whose return type is an rvalue reference is an xvalue. —end
    example ]

    And the beginning of §5 of the standard says:

    [ Note: An expression is an xvalue if it is:

    • the result of calling a
      function, whether implicitly or explicitly, whose return type is an
      rvalue reference to object type,
    • a cast to an rvalue reference to
      object type,
    • a class member access expression designating a
      non-static data member of non-reference type in which the object
      expression is an xvalue, or
    • a .* pointer-to-member expression in
      which the first operand is an xvalue and the second operand is a
      pointer to data member.

    In general, the effect of this rule is that
    named rvalue references are treated as lvalues and unnamed rvalue
    references to objects are treated as xvalues; rvalue references to
    functions are treated as lvalues whether named or not. —end note ]


    As an example, if NRVO can be done, it’s like this:

    void MoveAFoo(Foo* f) {
        new (f) Foo;
    }
    
    Foo myfoo; // pretend this isn't default constructed
    MoveAFoo(&myfoo);
    

    If NRVO can’t be done but Foo is moveable, then your example is a little like this:

    void MoveAFoo(Foo* fparam) {
        Foo f;
    
        new (fparam) Foo(std::move(f));
    }
    
    Foo f; // pretend this isn't being default constructed
    MoveAFoo(&f);
    

    And if it can’t be moved but it can be copied, then it’s like this

    void MoveAFoo(Foo* fparam) {
        Foo f;
    
        new (fparam) Foo((Foo&)f);
    }
    
    Foo f; // pretend this isn't default constructed
    MoveAFoo(&f);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can define 2 variables of the same type in a for loop: int
I can define default value in domain by this way : class ProcessingPriority {
In Java you can define generic class that accepts only types that extend a
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
In core.php I can define Configure::write('Routing.admin', 'admin'); and /admin/controller/index will work. but if I
Can classes have multiple constructors and/or copy constructors in common-lisp? That is - in
The question: Can I define an assignment operator and not the copy constructor? For
users can be dragged to nodes to move or copy a user from one
I can define church numerals fairly easy using scheme: > (define f (lambda (x)
In C# we can define a generic type that imposes constraints on the types

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.