Talk:More C++ Idioms/Non-copyable Mixin

There is a minor issue when using NonCopyable. Please consider:

In this case C inherits two sub-objects of the same base class NonCopyable. The standart requires sub-objects of the same base class to have distinct addresses, which prohibits empty base class optimization in this case and make the sizeof(C) at least 2.

To make empty base class optimization work something like the following can be done:

Here, C does not have any base class sub-objects of the same type, which allows for empty base class optimization.

NonCopyable::operator= gets hidden
In the following code:

The compiler generated CantCopy::operator= hides NonCopyable::operator=, thus making the latter useless. Assignment in the following code does not cause a compiler error as it should.

To fix the code NonCopyable::operator= signature needs to be changed to:


 * Make it "NonCopyable & operator = (const NonCopyable &);" and I agree. ;) --212.111.224.237 (talk) 15:39, 16 March 2010 (UTC)

193.77.185.88 (discuss) 10:54, 1 September 2011 (UTC) I think this needs to be updated to the new standard, using the =delete feature.

Does it get fixed by C++11 with constructor NonCopyable = default ?
Anyone has idea ?

What is the benefit of this idiom over deleting the copy constructor and assignment operator?
I cannot see why this idiom would be preferred over:

The reasons given in the article aren't convincing. I don't see how inheriting from another class is "simpler and cleaner" than deleting the copy constructor and assignment operator directly in the class declaration.

The meaning of deleting these functions is clear enough, and doesn't need any additional documentation. You might need to document why you are deleting these operations, but that's no better (or worse?) than documenting why you are inheriting from NonCopyable.

The idiom also creates the Banana Gorilla Forest problem, which doesn't occur in case of deleting the copy constructor and assignment operator. --Please-choose-another-username-666 (discuss • contribs) 05:58, 14 July 2019 (UTC)