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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:02:07+00:00 2026-05-19T11:02:07+00:00

So, after watching this wonderful lecture on rvalue references, I thought that every class

  • 0

So, after watching this wonderful lecture on rvalue references, I thought that every class would benefit of such a “move constructor”, template<class T> MyClass(T&& other) edit and of course a “move assignment operator”, template<class T> MyClass& operator=(T&& other) as Philipp points out in his answer, if it has dynamically allocated members, or generally stores pointers. Just like you should have a copy-ctor, assignment operator and destructor if the points mentioned before apply.
Thoughts?

  • 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-19T11:02:07+00:00Added an answer on May 19, 2026 at 11:02 am

    I’d say the Rule of Three becomes the Rule of Three, Four and Five:

    Each class should explicitly define exactly one
    of the following set of special member
    functions:

    • None
    • Destructor, copy constructor, copy assignment operator

    In addition, each class that explicitly defines a destructor may explicitly define a move constructor and/or a move assignment operator.

    Usually, one of the following sets of special member
    functions is sensible:

    • None (for many simple classes where the implicitly generated special member functions are correct and fast)
    • Destructor, copy constructor, copy assignment operator (in this case the
      class will not be movable)
    • Destructor, move constructor, move assignment operator (in this case the class will not be copyable, useful for resource-managing classes where the underlying resource is not copyable)
    • Destructor, copy constructor, copy assignment operator, move constructor (because of copy elision, there is no overhead if the copy assignment operator takes its argument by value)
    • Destructor, copy constructor, copy assignment operator, move constructor,
      move assignment operator

    Note:

    • That move constructor and move assignment operator won’t be generated for a class that explicitly declares any of the other special member functions (like destructor or copy-constructor or move-assignment operator).
    • That copy constructor and copy assignment operator won’t be generated for a class that explicitly declares a move constructor or move assignment operator.
    • And that a class with an explicitly declared destructor and implicitly defined copy constructor or implicitly defined copy assignment operator is considered deprecated.

    In particular, the following perfectly valid C++03 polymorphic base class:

    class C {
      virtual ~C() { }   // allow subtype polymorphism
    };
    

    Should be rewritten as follows:

    class C {
      C(const C&) = default;               // Copy constructor
      C(C&&) = default;                    // Move constructor
      C& operator=(const C&) = default;  // Copy assignment operator
      C& operator=(C&&) = default;       // Move assignment operator
      virtual ~C() { }                     // Destructor
    };
    

    A bit annoying, but probably better than the alternative (in this case, automatic generation of special member functions for copying only, without move possibility).

    In contrast to the Rule of the Big Three, where failing to adhere to the rule can cause serious damage, not explicitly declaring the move constructor and move assignment operator is generally fine but often suboptimal with respect to efficiency. As mentioned above, move constructor and move assignment operators are only generated if there is no explicitly declared copy constructor, copy assignment operator or destructor. This is not symmetric to the traditional C++03 behavior with respect to auto-generation of copy constructor and copy assignment operator, but is much safer. So the possibility to define move constructors and move assignment operators is very useful and creates new possibilities (purely movable classes), but classes that adhere to the C++03 Rule of the Big Three will still be fine.

    For resource-managing classes you can define the copy constructor and copy assignment operator as deleted (which counts as definition) if the underlying resource cannot be copied. Often you still want move constructor and move assignment operator. Copy and move assignment operators will often be implemented using swap, as in C++03. Talking about swap; if we already have a move-constructor and move-assignment operator, specializing std::swap will become unimportant, because the generic std::swap uses the move-constructor and move-assignment operator if available (and that should be fast enough).

    Classes that are not meant for resource management (i.e., no non-empty destructor) or subtype polymorphism (i.e., no virtual destructor) should declare none of the five special member functions; they will all be auto-generated and behave correct and fast.

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

Sidebar

Related Questions

After watching the Knockout.JS video with Steve Sanderson I decided this would be great
Introduction After watching this video from LIDNUG, about .NET code protection http://secureteam.net/lidnug_recording/Untitled.swf (especially from
After watching this video , I am wondering if I am using my controllers
After watching the very known video on this topic I decided to go with
After watching this video , I started thinking about how I could implement something
After watching RailsCasts #273 I want to use the Geocoder gem. I've seen this:
Google weather API shows temperature in Fahrenheit. After watching this post , I was
After watching Guido's Google IO talk and hearing the mention that it can be
We have recently switched to Mercurial. After watching this helpful video: http://www.youtube.com/watch?v=-k2vLKOUb8s&noredirect=1 I am
I have a service that opens multiple watchers to watch multiple folders. After watching

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.