Why should the code have an available copy / move constructor, even if copying is allowed?

Nicole Bolas wrote the following in his answer on SO:

Copying permissions was allowed in a number of cases. However, even if it was allowed, the code should still work as if the copy had not been deleted. Namely, there must be an available copy and / or move of the constructor.]

Why was it necessary (before the appearance of "guaranteed copy permission") for the code to support the copy / move constructor, even if copying was allowed?

Why does the “ guaranteed copy permission ” free the programmer from these requirements?

+5
source share
4 answers

If the elision copy is not guaranteed (or required) by the standard, then the compiler should not implement it.

This meant that standard permitted compilers supported copying, but did not require them. And in practice, a number of compiler providers have decided not to implement copying. For these suppliers, this is a matter of cost — not implementing a function requires less development effort. For programmers (people who use compilers), this was due to the quality of implementation - a better compiler is more likely to implement the desired optimization, including copying, than a better compiler, but also more expensive to purchase.

Over time, when better compilers become more accessible (with the help of various definitions “free” - not all are equivalent to zero cost), gradually the standard can specify more functions that were previously optional. But it did not start.

If copying is optional, some compilers will rely on the availability of appropriate copy constructors, etc., and some will not. However, the concept of code that meets the requirements of a standard that builds with one compatible compiler, but not another, is naturally undesirable in the standard. Therefore, the standard provides that designers should be available, even if they allow them to be implemented.

+3
source

In order for the code to be guaranteed to work, it must have some way to work without copy permission for each case where a copy exception is not guaranteed.

+1
source

Because it was just allowed, but not guaranteed.

If an accessible copy constructor is not required, some code will be compiled when the optimization starts, but may fail on another compiler.

+1
source

Why was it necessary (before the appearance of "guaranteed copy permission") for the code to support the copy / move constructor, even if copying was allowed?

Because, as others have said, it was simply allowed for the copy or move to be omitted. Not every compiler should have omitted it, so for consistency, the programmer still planned for copy / move to be possible. And conceptually, there was still copy / move, regardless of whether this was done or not by the compiler - this is a completely different story.

Why does “guaranteed copying” free the programmer from these requirements?

Because there is no copy or moving to start. A guaranteed copy of "elision" works by completely changing the value of T a = T() , saying that T() initializes a instead of a temporary object, so by no means copy or move constructors are part of the game.

0
source

All Articles