AC_DISABLE_MOVE

Reference

#include <actl/lifetime/move/AC_DISABLE_MOVE.hpp>
AC_DISABLE_MOVE(type)

Macro to be used inside a class to disable its move and copy constructor and assignment operator. For example,

class mutex {
public:
    mutex() = default;
    AC_DISABLE_MOVE(mutex)
};

mutex a;
mutex b = a;            // compilation error
mutex c = std::move(a); // compilation error

Default constructor is also disabled unless defined explicitly.

To disable only copy and not move, use AC_DISABLE_COPY.

See tests at tests/lifetime/move/AC_DISABLE_MOVE.cpp

Design

AC_DISABLE_MOVE has effect similar to boost::noncopyable. However, the base class approach of boost::noncopyable has multiple issues:

  • Extra base class makes class hierarchy more complex, which can reduce dynamic_cast efficiency.

  • If multiple empty classes derive from the same boost::noncopyable then their storage cannot be merged together using EBO.

  • Namespace of the base class is added to ADL, which adds extra work for the compiler and can have surprising effects.

  • Aggregate initialization requires extra braces for the base class: Derived{{}, members...}.

  • operators == and <=> cannot be defaulted, that is the following code won’t compile

    bool operator==(const Derived&) const = default;