operatorMoveOnly
operatorMoveOnly is a C++20 feature that allows a class to declare that its objects can only be moved, not copied. This is achieved by marking all copy constructor and copy assignment operator overloads as deleted. This signifies that the object's state is not intended to be duplicated, and operations that would involve copying are explicitly disallowed.
When a class has operatorMoveOnly semantics, attempting to copy an object of that class will result in
The primary use case for operatorMoveOnly is for resource management classes where duplication is either impossible,
To implement operatorMoveOnly, developers must explicitly delete the copy constructor and copy assignment operator. For instance,
MyClass(MyClass&&) = default; // Move constructor (optional, can be defaulted or custom)
MyClass& operator=(MyClass&&) = default; // Move assignment operator (optional, can be defaulted or custom)
MyClass(const MyClass&) = delete; // Delete copy constructor
MyClass& operator=(const MyClass&) = delete; // Delete copy assignment operator
// Other members...
};
This explicitly disallows copying while allowing moving, thus establishing the operatorMoveOnly behavior for instances of MyClass.