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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:57:30+00:00 2026-05-16T04:57:30+00:00

As I’ve asked in Move constructor/operator= and after a while I’ve agreed and accepted

  • 0

As I’ve asked in Move constructor/operator= and after a while I’ve agreed and accepted right answer to that question I was just thinking, if would it be useful to have something like “moving destructor” which would be invoked on moved object everytime we used move ctor or operator=.
In this way we would have to specify only in move dtor what we want from it and how our object shuld be nullified after being used by move constructor. Without this semantics it looks like everytime I write move ctor or operator= I have to explicitly state in each of them (code repetition/error introduction) how to nullify moved object which isn’t the best possible option I think. Looking forward to your opinions on this subject.

  • 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-16T04:57:30+00:00Added an answer on May 16, 2026 at 4:57 am

    Could you bring a concrete example, where it would be useful. E.g, as far as I understand, move assignment might in the general case be implemented as

    this->swap(rhv); 
    

    The swap method is probably beneficial in any case, if the class benefits from move semantics. This nicely delegates the work of releasing the old resources of *this to the regular destructor.

    Without particular examples that show a new kind of destructor is an elegant way of achieving correct code, your proposal doesn’t look very appealing.

    Also, according to the latest revisions, move constructor / assignment operator can be defaulted. This means that very likely
    my classes will look like this:

    class X
    {
        well_behaved_raii_objects;
    public:
        X(X&& ) = default;
        X& operator=(X&&) = default;
    }; 
    

    No destructor at all! What would make it appealing to me to have two destructors instead?

    Also take into account that the assignment operator has old resources to deal with. As per current standard you have to be careful that normal destructor call is fine both after construction and assignment, and IMO, similarly with the proposed move destructor you would have to take care in the constructor and the assignment operator that the same move destructor can be safely called. Or would you like two move destructors – one for each? 🙂


    Reworked example of the msdn example in the comments with move constructor/assignment

    #include <algorithm>
    
    class MemoryBlock
    {
    public:
    
       // Simple constructor that initializes the resource.
       explicit MemoryBlock(size_t length)
          : length(length)
          , data(new int[length])
       {
       }
    
       // Destructor.
       ~MemoryBlock()
       {
          delete[] data; //checking for NULL is NOT necessary
       }
    
       // Copy constructor.
       MemoryBlock(const MemoryBlock& other)
          : length(other.length)
          , data(new int[other.length])
       {
          std::copy(other.data, other.data + length, data);
       }
    
       // Copy assignment operator (replaced with copy and swap idiom)
       MemoryBlock& operator=(MemoryBlock other)  //1. copy resource
       {
           swap(other);  //2. swap internals with the copy
           return *this; //3. the copy's destructor releases our old resources
       }
    
       //Move constructor
       //NB! C++0x also allows delegating constructors
       //alternative implementation:
       //delegate initialization to default constructor (if we had one), then swap with argument
       MemoryBlock(MemoryBlock&& other)
        : length(other.length)
        , data(other.data)
        {
            other.data = 0; //now other can be safely destroyed
            other.length = 0; //not really necessary, but let's be nice
        }
    
        MemoryBlock& operator=(MemoryBlock&& rhv)
        {
            swap(rhv);
            //rhv now contains previous contents of *this, but we don't care:
            //move assignment is supposed to "ruin" the right hand value anyway
            //it doesn't matter how it is "ruined", as long as it is in a valid state
            //not sure if self-assignment can happen here: if it turns out to be possible
            //a check might be necessary, or a different idiom (move-and-swap?!)
            return *this;
        }
    
    
       // Retrieves the length of the data resource.
       size_t Length() const
       {
          return length;
       }
    
       //added swap method (used for assignment, but recommended for such classes anyway)
       void swap(MemoryBlock& other) throw () //swapping a pointer and an int doesn't fail
       {
            std::swap(data, other.data);
            std::swap(length, other.length);
        }
    
    private:
       size_t length; // The length of the resource.
       int* data; // The resource.
    };
    

    Some comments on the original MSDN sample:

    1) checking for NULL before delete is unnecessary (perhaps it is done here for the output which I have stripped, perhaps it indicates a misunderstanding)

    2) deleting resources in the assignment operator: code reduplication. With the copy-and-swap idiom deleting previously held resources is delegated to the destructor.

    3) copy-and-swap idiom also makes self-assignment checks unnecessary. It is not a problem if the resource is copied before it is deleted. – (“Copy the resource regardless” on the other hand only hurts when you expect lots of self-assignments done with this class.)

    4) Assignment operator in the MSDN’s example lacks any kind of exception safety: if allocating new storage fails, the class is left in an invalid state with an invalid pointer. Upon destruction undefined behavior will happen.

    This could be improved by carefully reordering the statements, and setting the deleted pointer to NULL in-between (unfortunately it seems that the invariant of this particular class is that it always holds a resource, so having it cleanly lose the resource in case of an exception isn’t perfect either). By contrast, with copy-and-swap, if an exception happens, left-hand value remains in its original state (much better, operation can’t be completed but data loss is avoidable).

    5) The self-assignment check looks particularly questionable in the move assignment operator. I don’t see how left-hand-value could be the same as the right-hand-value in the first place. Would it take a = std::move(a); to achieve identity (looks like it would be undefined behavior anyway?)?

    6) Again, move assignment is unnecessarily managing resources, which my version simply delegates to the regular destructor.

    Conclusion: the code reduplication you are seeing is avoidable, it is only introduced by a naive implementation (one which you, for some reason, tend to see in tutorials, probably because code with reduplications is easier to follow for learners).

    To prevent resource leaks, always free
    resources (such as memory, file
    handles, and sockets) in the move
    assignment operator.

    … if code reduplication is fine by you, otherwise reuse the destructor.

    To prevent the unrecoverable
    destruction of resources, properly
    handle self-assignment in the move
    assignment operator.

    … or make sure you never delete anything before you are sure you can replace it.
    Or rather a SO question: is it possible for self-assignment to happen in case of move assignment in a well-defined program.


    Furthermore, from my draft (3092) I find that if a class has no user-defined copy constructor / assignment operator and nothing prevents the existence of a move-constructor / assignment, one will be declared implicitly as defaulted. If I’m not mistaken, this means: if the members are things like strings, vector, shared_ptrs etc, in which case you normally wouldn’t write a copy constructor / assignment, you’ll get a move constructor / move assignment for free.

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

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Putting aside the denormalized nature of this table (which you've… May 16, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Try: $("#emailList").change(function() { alert($('option:selected', $(this)).text()); }); May 16, 2026 at 11:29 am
  • Editorial Team
    Editorial Team added an answer Try merging the tables using UNION ALL to return the… May 16, 2026 at 11:29 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.