Scott Meyers has a
good point on the rule of zero . Basically, he stands for the default assignment / creation / move, regardless of whether they really need you. Basically, the general rule is to avoid generating a compiler for these members, mainly because they are a big source of confusion (I agree with that).
So, I was thinking about good general practice in how to define a class as movable, copyable or non-removable, not copyable. I was thinking about boost boost::noncopyable , but I don't like the idea of ββintroducing inheritance for such a functional purpose.
The only thing I can think of makes sense is to resort to macros. So I came up with something like this:
/// Disable copy construct/assign for the given class T
And an example of how to use them:
class foo { public: foo() = default; virtual ~foo() = default; CLASS_NON_COPYABLE(foo); CLASS_DEFAULT_MOVABLE(foo); }; int main() { foo a, b; a = b;
( Live sample )
For me, this looks a lot cleaner than the alternative:
class foo { public: foo() = default; virtual ~foo() = default; foo(foo const&) = delete; foo(foo&&) = default; foo& operator=(foo const&) = delete; foo& operator=(foo&&) = default; };
This is debatable, as most of the style-based issues are there, but I find the advantage in the former:
- Macros are searchable, so you can find classes that use move / copy semantics in different ways without using a complex regular expression.
- It is natural to
#ifdef transfer / copy semantics in old compilers through the #ifdef logic, where C ++ 11 is not available (for example, CLASS_NON_MOVABLE will be inoperative). - It's easier to look at your eyes to see what the class is doing.
- And it's easier to type :-)
Given that I do a lot of magic here, my soul hurts a little, and I feel the need for compulsory "SO-mail" to see if I really do it efficiently and not lose my mind. "
So, I have a few closely related questions:
- Am I deleting templates in an ideal and efficient way?
- Are there any better / other or more recommended methods for this. I would even be open to elegant inheritance solutions similar to what Boost provides.
Maybe I'll do it all wrong. Perhaps this whole question is doomed to failure, and I am above thoughts. I would also like to rethink all of this, just in case I went too far down the rabbit hole.