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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:20:56+00:00 2026-06-13T11:20:56+00:00

Suppose we have this class: class X { public: explicit X (char* c) {

  • 0

Suppose we have this class:

class X {
public:
    explicit X (char* c) { cout<<"ctor"<<endl; init(c); };
    X (X& lv)  { cout<<"copy"<<endl;  init(lv.c_); };
    X (X&& rv) { cout<<"move"<<endl;  c_ = rv.c_; rv.c_ = nullptr; };

    const char* c() { return c_; };

private:
    void init(char *c) { c_ = new char[strlen(c)+1]; strcpy(c_, c); };
    char* c_;

};

and this sample usage:

X x("test");
cout << x.c() << endl;
X y(x);
cout << y.c() << endl;
X z( X("test") );
cout << z.c() << endl;

The output is:

ctor
test
copy
test
ctor   <-- why not move?
test

I am using VS2010 with default settings. I’d expect the last object (z) to be move-constructed, but it’s not! If I use X z( move(X("test")) ); then the last lines of the output are ctor move test, as I’d expect. Is it a case of (N)RVO?

Q: Should the move-ctor be called according to the standard? If so, why isn’t it called?

  • 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-13T11:20:57+00:00Added an answer on June 13, 2026 at 11:20 am

    What you are seeing is copy elision, which allows the compiler to directly construct a temporary into a target it is to be copied/moved into and thus elide a copy (or move) constructor/destructor pair. The situations in which the compiler is allowed to apply copy elision are specified in §12.8.32 of the C++11 standard:

    When certain criteria are met, an implementation is allowed to omit
    the copy/move construction of a class object, even if the copy/move
    constructor and/or destructor for the object have side effects. In such
    cases, the implementation treats the source and target of the omitted
    copy/move operation as simply two different ways of referring to the
    same object, and the destruction of that object occurs at the later of
    the times when the two objects would have been destroyed without the
    optimization. This elision of copy/move
    operations, called copy elision, is permitted in the following
    circumstances (which maybe combined to eliminate multiple copies):

    • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with
      the same cv-unqualified type as the function return type, the
      copy/move operation can be omitted by constructing the automatic
      object directly into the function’s return value
    • in a throw-expression, when the operand is the name of a non-volatile automatic object whose scope does not extend beyond
      the end of the innermost enclosing try-block (if there is one), the
      copy/move operation from the operand to the exception object (15.1)
      can be omitted by constructing the automatic object directly into
      the exception object
    • when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with he
      same cv-unqualified type, the copy/move operation can be omitted by
      constructing the temporary object directly into the target of the
      omitted copy/move
    • when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as
      the exception object (15.1), the copy/move operation can be omitted
      bytreatingthe exception-declaration as an alias for the exception
      object if the meaning of the program will be unchanged except for the
      execution of constructors and destructors for the object declared by
      the exception-declaration.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this class: class foo { public: foo() { } foo(const std::string&
Suppose I have something like this: class A { public B mem; public int
Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
Suppose you have this class: public class A { private int number; public setNumber(int
Suppose I have this: public class Unit<MobileSuit, Pilot> { ... List<MobileSuit> mobileSuits; List<Pilot> pilots;
Let's suppose you have this class: class A { public: A () {} A
Suppose we have this problem public class Father{ public void method1(){...} } public class
Suppose I have some code like this: class Base { public: virtual int Foo(int)
Suppose I have this annotation class @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface MethodXY { public int
Suppose I have this model: public class ViewModel { [Required] public string UserInput {

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.