AC_DISABLE_COPY¶
Reference¶
#include <actl/lifetime/copy/AC_DISABLE_COPY.hpp>
-
AC_DISABLE_COPY(type)¶
Macro to be used inside a class to disable its copy constructor and assignment operator while allowing to define custom move constructor and assignment operator. For example,
class unique_ptr { void* pointer = nullptr; public: unique_ptr() = default; AC_DISABLE_COPY(unique_ptr) unique_ptr(unique_ptr&& that) { this = std::move(*that); } unique_ptr& operator=(unique_ptr&& that) { this->poiner = that->pointer; that->poiner = nullptr; return *this; } }; unique_ptr a; unique_ptr b = a; // compilation error unique_ptr c = std::move(a); // ok
Default constructor is also disabled unless defined explicitly.
Note
Move constructor and assignment operator are disabled if not defined. If that’s the intention then it’s more descriptive to use AC_DISABLE_MOVE.
See tests at tests/lifetime/copy/AC_DISABLE_COPY.cpp