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