tags:

views:

91

answers:

5

To prohibit copy construction and copy assignment, I've seen the boost noncopyable class and in the Google style guide the DISALLOW_COPY_AND_ASSIGN macro. Is there any reason to prefer one of the techniques over the other, or any subtle differences one should be aware of?

+5  A: 

I prefer boost noncopyable over the macro because it's not a macro and it is easier (IMO) to use.

In real code, i'm using neither of them and write the two declarations needed myself.

A subtle difference you might be interested in, though, is that using that macro or your own declarations will not give a compiler error when the class itself tries to copy the object (but rather fail to link later on). noncopyable however will also signal a compiler error in this case, because it's the base-class that has the functions declared private, not the class itself.

Johannes Schaub - litb
Another small advantage about using `noncopyable` is that the error message produced by the compiler will include the word "noncopyable", which might make it easier to track down what the error actually is. (I don't know how this compares to the `DISALLOW_COPY_AND_ASSIGN`-macro, which I have never used)
Magnus Hoff
@Magnus ah good point!
Johannes Schaub - litb
+2  A: 

Based on the rule of avoid inheritance wherever possible, I always use a macro - in this case my own. Of course, there is the other rule, about avoiding macros...

anon
I really see no harm in privately inheriting from `boost::noncopyable`...? Could you elaborate?
FredOverflow
+3  A: 

I always make the copy constructor and assignment operator private, without implementation:

  • this prevents someone making copies outside the class (will give compile errors)
  • this prevents the class itself also from making copies of itself (will give link errors)

I also add a clear comment (just above the prototypes of the copy constructor and assignment operator) to indicate that I do this to prevent someone making copies (as a reminder to future developers).

In my opinion, this solution is clear, does not rely on external libraries or macro's and is easy to understand.

Patrick
It does have the disadvantage that its not easily searched for - both the use of derivation and the use of a macro can easily be found via text searches.
anon
@Neil the comments provide excellent tools for finding them by using searches (i don't think i ever had the need to look-up all non-copyable classes, though).
Johannes Schaub - litb
@Johannes Only if they contain standardised text, in which case I have to wonder why bother.
anon
A: 

The two techniques are [practically] the same. One uses a macro to save typing (you still have to place it where you would normally place the code) and the other provides the facility through a base class that you extend.

Avoiding macros is a good thing but extending a class just to avoid placing the copy constructor in the private section of your class seems confusing as a reader.

Any reason you dont just do what these two methods do within your class?

ezpz
+2  A: 

I would never use the DISALLOW_COPY_AND_ASSIGN macro because you have to put it in the private part of the class declaration. What if you (or someone maintaining your code) by mistake put it in the public part? The problem is that it is not obvious at all that "DISALLOW_COPY_AND_ASSIGN" should be put in the private part: the name of the macro seems to imply that it will disallow copy and assignment independently of where you have declared it.

The noncopyable base class avoids this issue entirely.

However, the fastest, most readable, and portable way is to explicitly declare the constructors in the private part of the class.

Lorenzo
What do you mean by "fastest"? What is particularly slow about using for example `boost::noncopyable`? And what is nonportable about either of the proposed solutions?
Magnus Hoff
I mean fastest for you to implement... Non portable means that you rely in both case on non standard code.
Lorenzo
In that case, I respectfully disagree. I think `boost::noncopyable` is faster to write, and it requires less thinking. It is also portable, and does not rely on non-standard code. (But you do of course need the `noncopyable.hpp`-file) (I understand "non-standard code" as code that violates the C++ standard)
Magnus Hoff
There is no "non-standard code" in either case.
anon
Of course "non standard code" is the short version for "code that is not in the standard library" (I never worked on a real-life project that used boost). This also means faster since you don't have to handle a dependency.Requiring less thinking isn't usually a good thing. And this is not even thinking, is just declaring two constructors.
Lorenzo