Consider a classic approach to make a class non copyable:
// similar to boost::noncopyable
class noncopyable
{
protected:
constexpr noncopyable() = default;
noncopyable(const noncopyable&) = delete;
noncopyable& operator=(const noncopyable&) = delete;
};
class c: private noncopyable
{ /* ... */ };
As declaring any copy operation prevents auto-generation of move operations, this automatically makes all derived classes non moveable (by default) also (so full name of noncopyable
would be noncopyable_and_nonmoveable
).
Now let's look for classic noncopyable classes from the standard library, e.g. unique_ptr
. It's noncopyable but moveable, otherwise its utility would be limited. I suppose same is true for many other cases.
Actually I cannot come up with any example when a class should be made non moveable. My argument is: even if a class is not planned to be moved there's a little chance this would be done by a mistake that can cause a harm.
It's actually two related questions:
1) Why boost::noncopyable
is non moveable also? This causes issues, consider:
struct c: private boost::noncopyable
{
std::unique_ptr<Something> something;
c(c&&) = default;
c& operator=(c&&) = default;
};
doesn't work (wandbox) - move operations cannot be generated because they are not generated for the base class. You need to implement them manually -> list your members -> maintenance -> bugs. Just defaulting move operations in noncopyable
would solve the problem.
2) What are use cases when moveability is harmful so it should be prevented?