The following surely works but is very tedious:
T(const T&) = delete;
T(T&&) = delete;
T& operator=(const T&) = delete;
T& operator=(T&&) = delete;
I'm trying to discover the most concise way. Will the following work?
T& operator=(T) = delete;
Update
Note that I choose T& operator=(T)
instead of T& operator=(const T&)
or T& operator=(T&&)
, because it can serve both purposes.
According to this chart (by Howard Hinnant):
The most concise way is to =delete
move assignment operator (or move constructor, but it can cause problems mentioned in comments).
Though, in my opinion the most readable way is to =delete
both copy constructor and copy assignment operator.
You can write a simple struct
and inherit from it:
struct crippled
{
crippled() = default;
crippled(const crippled&) = delete;
crippled(crippled&&) = delete;
crippled& operator=(const crippled&) = delete;
crippled& operator=(crippled&&) = delete;
};
Usage:
struct my_class : crippled
{
};
int main()
{
my_class a;
auto b = a; // fails to compile
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With