Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my copy constructor only called twice in this scenario?

I have the following two functions:

Class foo(Class arg)
{
    return arg;
}

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

Now, when I solely call foo(arg), the copy constructor is of course called twice. When I call bar(&arg) solely, it's only called once. Thus, I would expect

foo(bar(&arg));

the copy constructor being called three times here. However, it's still only called twice. Why is that? Does the compiler recognise that another copy is unneeded?

Thanks in advance!

like image 345
Steffen Avatar asked May 12 '13 11:05

Steffen


People also ask

Why is my copy constructor not being called?

The reason the copy constructor is not called is because the copy constructor itself is a function with one parameter. You didn't call such function,so it didn't execute.

How many times is copy constructor called?

You call the function by value and do two copies inside.

What are two situations where a copy constructor may be called?

In C++, a Copy Constructor may be called for the following cases: 1) When an object of the class is returned by value. 2) When an object of the class is passed (to a function) by value as an argument. 3) When an object is constructed based on another object of the same class.

How many arguments can a copy constructor have?

A copy constructor always takes one parameter, reference to the type for which it belongs, there maybe other parameters but they must have default values.


1 Answers

Does the compiler recognise that another copy is unneeded?

Indeed it does. The compiler is performing copy/move elision. That is the only exception to the so called "as-if" rule, and it allows the compiler (under some circumstances, like the one in your example) to elide calls to the copy or move constructor of a class even if those have side effects.

Per paragraph 12.8/31 of the C++11 Standard:

When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization. This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):

— in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv-unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

— [...]

— when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

— [...]

With GCC you can try using the -fno-elide-constructor compilation flag to suppress this optimization and see how the compiler would behave when no copy elision occurs.

like image 193
Andy Prowl Avatar answered Sep 21 '22 10:09

Andy Prowl