C ++ 11 move constructor with side effects

In C ++, you cannot rely on a copy constructor that is called from the return statement because of a special clause in the standard that allows the compiler to omit the copy constructor call as a result of the return statement, even if the copy constructor has side effects. Thus, it is a bad style to write a copy constructor that does something else, and not just copy an instance of an instance.

Are there similar statements in the C ++ 11 standard that allow the compiler to exclude a call to the move constructor in certain circumstances - and if so, what are these circumstances?

+6
source share
3 answers

Copy-elision is used in exactly the same way to move a construct; it is the same sentence as both the exception of the copy construct and the move construct are collectively called β€œcopy”.

Β§12.8 [class.copy] p31

When certain criteria are met, implementations are allowed to omit the copy / move object of the class object, even if the copy / move constructor and / or a destructor for an object has side effects. [...]

+9
source

If copying is enabled, copying will not be performed, so the call to the constructor will not move, even if the object is moving. So a copy of the elite wins the movement, and you cannot be sure (at least not in a portable way) when this happens. Thus, this is one of the scenarios when side effects when making a copy copy will be a bad idea.

+3
source

Elision is defined the same for both copying and moving. The standard does not have any specific wording for movement, because they are defined identically.

+1
source

Source: https://habr.com/ru/post/922019/


All Articles