Reputation: 62185
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)?
Upvotes: 5
Views: 228
Reputation: 41509
It may be called. It also may be optimized away. See some other question in the same direction.
Upvotes: 2
Reputation: 72356
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.
Upvotes: 10
Reputation: 84169
Depends on the Return Value Optimization being applied or not.
Upvotes: 8