Why is my copy constructor called twice in this script?

I have the following two functions:

Class foo(Class arg) { return arg; } Class bar(Class *arg) { return *arg; } 

Now, when I only call foo (arg), the copy constructor, of course, is called twice. When I call bar (& arg) exclusively, it only calls once. So I would expect

 foo(bar(&arg)); 

copy constructor that is called here three times. However, it is still called only twice. Why is this? Does the compiler acknowledge that another copy is not needed?

Thanks in advance!

+6
source share
1 answer

Does the compiler acknowledge that another copy is not needed?

Indeed it is. The compiler executes the copy / move elision command. This is the only exception to the so-called as-if rule, and it allows the compiler (in some circumstances, for example, in your example) to delete calls to an instance or move the class constructor, even if they have a side to the consequences.

In paragraph 12.8 / 31 of the C ++ 11 standard:

When certain criteria are met, the implementation allows you to omit the copy / move construction of the object class , even if the constructor selected for the copy / move operation and / or the destructor for the object has side effects . In such cases, the implementation considers the source and purpose of the omitted copy / move as simply two different ways to access the same object and the destruction of this object occurs in later times, when two objects would be destroyed without optimization. This permission of copy / move operations, called elision copy , is permitted in the following cases (which can be combined to eliminate multiple copies):

- in the return expression in a function with the type of the returned class, when the expression is the name of a non-volatile automatic object (except for the function or parameter catch-clause) with the same cv-unqualified type as the returned type of the function, the copy / move operation can be omitted when building an automatic object directly in function returns value

- [...]

- if the temporary object of the class that was not attached to the link (12.2) is copied / moved to the class object with the same cv-unqualified type, the copy / move operation can be omitted from building the temporary object directly to the target of the missed copy / move

- [...]

With GCC, you can try using the compilation flag -fno-elide-constructor to suppress this optimization and see how the compiler will work when copy elimination does not occur.

+6
source

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


All Articles