With explicitly deleted member functions in C++11, is it still worthwhile to inherit from a noncopyable base class?
I'm talking about the trick where you privately inherit a base class which has private or deleted copy constructor and copy assignment (e.g. boost::noncopyable
).
Are the advantages put forward in this question still applicable to C++11?
I don't get why some people claim it's easier to make a class non-copyable in C++11.
In C++03:
private: MyClass(const MyClass&) {} MyClass& operator=(const MyClass&) {}
In C++11:
MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&) = delete;
EDIT:
As many people have pointed out, it was a mistake to provide empty bodies (i.e. {}) for the private copy constructor and copy assignment operator, because that would allow the class itself invoke those operators without any errors. I first started out not adding the {}, but ran into some linker issues that made me add the {} for some silly reason (I don't remeber the circumstances). I know better know. :-)
Well, this:
private: MyClass(const MyClass&) {} MyClass& operator=(const MyClass&) {}
Still technically allows MyClass
to be copied by members and friends. Sure, those types and functions are theoretically under your control, but the class is still copyable. At least with boost::noncopyable
and = delete
, nobody can copy the class.
I don't get why some people claim it's easier to make a class non-copyable in C++11.
It's not so much "easier" as "more easily digestible".
Consider this:
class MyClass { private: MyClass(const MyClass&) {} MyClass& operator=(const MyClass&) {} };
If you are a C++ programmer who has read an introductory text on C++, but has little exposure to idiomatic C++ (ie: a lot of C++ programmers), this is... confusing. It declares copy constructors and copy assignment operators, but they're empty. So why declare them at all? Yes, they're private
, but that only raises more questions: why make them private?
To understand why this prevents copying, you have to realize that by declaring them private, you make it so that non-members/friends cannot copy it. This is not immediately obvious to the novice. Nor is the error message that they will get when they try to copy it.
Now, compare it to the C++11 version:
class MyClass { public: MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&) = delete; };
What does it take to understand that this class cannot be copied? Nothing more than understanding what the = delete
syntax means. Any book explaining the syntax rules of C++11 will tell you exactly what that does. The effect of this code is obvious to the inexperienced C++ user.
What's great about this idiom is that it becomes an idiom because it is the clearest, most obvious way to say exactly what you mean.
Even boost::noncopyable
requires a bit more thought. Yes, it's called "noncopyable", so it is self-documenting. But if you've never seen it before, it raises questions. Why are you deriving from something that can't be copied? Why do my error messages talk about boost::noncopyable
's copy constructor? Etc. Again, understanding the idiom requires more mental effort.
The first thing is that as others before me point out, you got the idiom wrong, you declare as private and don't define:
class noncopyable { noncopyable( noncopyable const & ); noncopyable& operator=( noncopyable const & ); };
Where the return type of the operator=
can be basically anything. At this point, if you read that code in a header, what does it actually mean? It can only be copied by friends or it cannot be copied ever? Note that if you provide a definition as in your example, it is stating that I can be copied only inside the class and by friends, it is the lack of a definition what translates this into I cannot be copied. But the lack of a definition in the header is not a synonym for a lack of definition everywhere, as it could be defined in the cpp file.
This is where inheriting from a type called noncopyable makes it explicit that the intention is to avoid copies, in the same way that if you manually wrote the code above you should document with a comment in the line that the intention is disabling copies.
C++11 does not change any of this, it just makes the documentation explicit in the code. In the same line that you declare the copy constructor as deleted you are documented that you want it fully disabled.
As a last comment, the feature in C++11 is not just about being able to write noncopyable with less code or better, but rather about inhibiting the compiler from generating code that you don't want generated. This is just one use of that feature.
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