Suppose you have a functions like this:
Foo foo() {
Foo foo;
// more lines of code
return foo; // is the copy constructor called here?
}
Foo bar() {
// more lines of code
return Foo(); // is the copy constructor called here?
}
int main() {
Foo a = foo();
Foo b = bar();
}
When any of the functions return, is the copy constructor called (suppose there would be one)?
It might be called, or it might not be called. The compiler has the option of using the Return Value Optimization in both cases (though the optimization is a bit easier in bar
than in foo
).
Even if RVO eliminates the actual calls to the copy constructor, the copy constructor must still be defined and accessible.
Depends on the Return Value Optimization being applied or not.
It may be called. It also may be optimized away. See some other question in the same direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With