If I have a move-only class which is initially owned by AManager, can be transferred to another owner via obtain
, returned back to AManager via release
.
class AManager;
class A {
public:
A(const A& other) = delete;
const A& operator=(const A&) = delete;
A(A&& other);
A& operator=(A&& other);
private:
AManager& _manager;
bool _isValid;
};
class AManager {
public:
A obtain() {
return std::move(_instance);
}
void release(A a) {
_instance = std::move(a);
}
private:
A _instance;
};
Is it valid for A to move itself within its own destructor? i.e., is it valid for A to release itself back to its manager when it is destroyed?
A::~A() {
if (_isValid) {
_manager.release(std::move(*this));
}
}
Assuming the A class has some way to know if its in a valid state. For example, I added a valid flag. I believe unique_ptr uses the pointer being null or not.