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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:44:14+00:00 2026-05-23T11:44:14+00:00

During the implementation of the move constructor of a toy class, I noticed a

  • 0

During the implementation of the move constructor of a toy class, I noticed a pattern:

array2D(array2D&& that)
{
    data_ = that.data_;
    that.data_ = 0;

    height_ = that.height_;
    that.height_ = 0;

    width_ = that.width_;
    that.width_ = 0;

    size_ = that.size_;
    that.size_ = 0;
}

The pattern obviously being:

    member = that.member;
    that.member = 0;

So I wrote a preprocessor macro to make stealing less verbose and error-prone:

#define STEAL(member) member = that.member; that.member = 0;

Now the implementation looks as following:

array2D(array2D&& that)
{
    STEAL(data_);
    STEAL(height_);
    STEAL(width_);
    STEAL(size_);
}

Are there any downsides to this? Is there a cleaner solution that does not require the preprocessor?

  • 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-23T11:44:15+00:00Added an answer on May 23, 2026 at 11:44 am

    Here is the recommended pattern:

    array2D(array2D&& that)
        : data_(std::move(that.data_)),
          height_(std::move(that.height_)),
          width_(std::move(that.width_)),
          size_(std::move(that.size_))
    {
        that.data_ = 0;
        that.height_ = 0;
        that.width_ = 0;
        that.size_ = 0;
    }
    

    Naturally if the data members are scalar types, the std::move isn’t needed. But if you’re copying this pattern around, it is helpful to include the move anyway so that when the member data aren’t scalar, the std::move doesn’t get forgotten.

    Also if the member data have actual move constructors, then you can simply omit the body:

    array2D(array2D&& that)
        : data_(std::move(that.data_)),
          height_(std::move(that.height_)),
          width_(std::move(that.width_)),
          size_(std::move(that.size_))
    {
    }
    

    And if you want to generalize to types that don’t have move constructors, but do have a resource-less default constructed state, you can:

    array2D(array2D&& that)
        : data_(std::move(that.data_)),
          height_(std::move(that.height_)),
          width_(std::move(that.width_)),
          size_(std::move(that.size_))
    {
        that.data_ = Data();
        that.height_ = Height();
        that.width_ = Width();
        that.size_ = Size();
    }
    

    I recommend ordering these statements in the same order they are declared as data members in the array2D class definition. And I find nothing wrong with the repetition of the initializer list in the body. It is a necessary and second step. There is no need to sweep it under the rug.

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

Sidebar

Related Questions

I work on someone else's code and it uses Transacted during class member implementation,
I am trying queue implementation in c++. During that I am having this problem.
I looked into the implementation of Array.Resize() and noticed that a new array is
During testing, I'm stuck with testing a piece of code that receives a list
Is there a concept in C# of class definition and implementation similar to what
Thanks for helping. The errors occurs during implementation accepts_nested_attributes_for . I get ActionView::Template:Error (undefined
Greetings, I am having a lot of issues during implementation of a model driven
During the design phase of system development, should the class design dictate the design
I have a set of objects that, during some initializations, I need to keep
Looking at the implementation on Wikipedia, it would seem that a standard BST (non

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.