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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:25:48+00:00 2026-06-08T16:25:48+00:00

I read a beautiful article on the move semantics in C++11. This article is

  • 0

I read a beautiful article on the move semantics in C++11. This article is written in a very intuitive way. The example class in the article is given below.

class ArrayWrapper 
{ 
public: 
    // default constructor produces a moderately sized array 
    ArrayWrapper () 
        : _p_vals( new int[ 64 ] ) 
        , _metadata( 64, "ArrayWrapper" ) 
    {} 

    ArrayWrapper (int n) 
        : _p_vals( new int[ n ] ) 
        , _metadata( n, "ArrayWrapper" ) 
    {} 

    // move constructor 
    ArrayWrapper (ArrayWrapper&& other) 
        : _p_vals( other._p_vals  ) 
        , _metadata( other._metadata ) 
    { 
        other._p_vals = NULL; 
    } 

    // copy constructor 
    ArrayWrapper (const ArrayWrapper& other) 
        : _p_vals( new int[ other._metadata.getSize() ] ) 
        , _metadata( other._metadata ) 
    { 
        for ( int i = 0; i < _metadata.getSize(); ++i ) 
        { 
            _p_vals[ i ] = other._p_vals[ i ]; 
        } 
    } 
    ~ArrayWrapper () 
    { 
        delete [] _p_vals; 
    } 
private: 
    int *_p_vals; 
    MetaData _metadata; 
};

Clearly in the above move constructor implementation, the movement doesn’t happen for the the embedded element _metadata. To facilitate this the trick is to use the std::move() method like this.

ArrayWrapper (ArrayWrapper&& other) 
        : _p_vals( other._p_vals  ) 
        , _metadata( std::move( other._metadata ) ) 
{ 
    other._p_vals = NULL; 
} 

So far, so good.

The standard says:

§5 (C++11 §5[expr]/6):

[ 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.

My question:

Now, the variable other in the move constructor is an xvalue (am I right?). Then according to the last rule above, other._metadata should also be an xvalue. And hence the compiler can implicitely use the move constructor of _metadata‘s class. So, no need to std::move here.

What am I missing?

  • 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-08T16:25:49+00:00Added an answer on June 8, 2026 at 4:25 pm

    Your assumption is not really true. The argument to the constructor is an xvalue, which allows the rvalue-reference to be bound, but once the rvalue-reference is bound, inside the constructor, it is no longer an xvalue but an lvalue. Conceptually, the object at the call place is expiring, but inside the constructor and until it completes it is no longer expiring, as it can be used later within the constructor block.

    ArrayWrapper f();
    ArrayWrapper r = f();   // [1]
    

    In [1], the expression f() refers to a temporary, that will expire after the call to the constructor, so it can be bound by an rvalue-reference.

    ArrayWrapper (ArrayWrapper&& other) 
        : _p_vals( other._p_vals  ) 
        , _metadata( other._metadata )        // [2] 
    { 
        other._p_vals = NULL; 
        std::cout << other._metadata << "\n"; // [3]
    } 
    

    Inside the constructor, other is not expiring, it will be alive for each and every instruction of the constructor. If the compiler allowed moving in [2], than a potential further use of the variable in [3] would be invalid. You have to explicitly tell the compiler that you want the value to expire now.

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

Sidebar

Related Questions

I've read the Beautiful code with Google Collections, Guava and static imports article about
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
I read this entire post . It describes what a wrapper class is to
Read on before you say this is a duplicate, it's not. (as far as
Read about the issue in this stackoverflow question . Still have the same issue
I read an article about a regular expression to detect base64 but when I
I have read about this from several places. I have tried the method found
This is related to a chapter from beautiful code . And in that chapter
I recent read again the beautiful intro to recursion in the 'simply scheme' book

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.