0

What is the automatically generated move assignment operator for a class Number that contains only one attribute of type std::shared_ptr<Vertex>?

#include <iostream>
#include <memory>

class Vertex{
    Vertex(Vertex&&)=delete;
    Vertex(Vertex const&)=delete;
    void operator=(Vertex&&)=delete;
    void operator=(Vertex const&)=delete;
private:
    const size_t index;
public:
    Vertex()/*.index initialization.*/{}
};

class Number{
private:
    mutable std::shared_ptr<Vertex> vtx=NULL;
public:
    Number(){}
    const Number& operator=(const Number& rhs ){
        if(vtx==NULL){ vtx=std::shared_ptr<Vertex>( new Vertex() ); }
        // ...
        return *this;
    }
    //const Number& operator=(Number&& rhs) = delete; // <-- critical line
    const Number& operator+=(const Number& rhs);
};

Number operator+(const Number& a, const Number& b){
    Number x;
    // ...
    return x;
}

const Number& Number::operator+=(const Number& rhs){
    return (*this + rhs);
}


int main() {
    Number u,v,w;
    w = u;
    w += v;
    return 0;
}

We have been using this code and it yielded the intended behaviour when the critical line is commented out. We had been unaware that the compiler is generating this operator automatically. Now, because we cannot delete it since it is actually being critical for the program to function, we would like to know precisely its code. Can someone state it?

We have tried a couple of implementations ourselves that yield an unintended behaviour. Since the automatically generated one does precisely what we need, we would really like to identify it.

9
  • 5
    It's not being generated because operator=(const Number& rhs ) exists. Deleting it explicitly is harmful and doesn't do anything useful, it will just give you errors when the rhs is a mutable rvalue. Commented Nov 28, 2023 at 9:31
  • @HolyBlackCat Thank you. Okay so I run into the special case of rhs being a mutable rvalue and that is relevant for the issue. This is good to know! Now what I then don't undestand is how deletion of something that is not generated can be harmful. Since it is not generated, deletion should mean no alteration to the class definition from my present incorrect understanding. What am I missing? Commented Nov 28, 2023 at 9:35
  • 3
    Deleted functions still participate in overload resolution (and cause a compilation error when selected by it). If you do this: Number foo(); Number n; n = foo();, if you don't delete the move assignment, the copy assignment will silently be used in its place, which is good. But if you do delete it, the deleted version is a better match, and you get a build error. Commented Nov 28, 2023 at 9:39
  • @HolyBlackCat. Ah!!, this makes so much sense. So the logical argument for the design is that with deleting a function/operator one not foremost intends to prevent its generation, but to flag that it is not to be intended to used or even resolved. Wow, and I cannot even explicitize it by defining the operator with code myself, because I do not have the object Number const& rhs at hand but only a Number&&. Commented Nov 28, 2023 at 9:43
  • 1
    It's perhaps worth mentioning that a deleted constructor (and others) is considered to be user-defined. It pretty much exists (if you look for it) and doesn't exist (if you try to use it) at the same time. Commented Nov 28, 2023 at 10:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.