Is there a difference in the number of temp objects created between these 2 functions?
string foo1() {
return "";
}
string foo2() {
string s = "";
return s;
}
This is a homework question so please assume there is no compiler optimization.
No- only one temporary is created. The object on the stack of the function is not a temporary, it is an lvalue. The string literal is also an lvalue. Both involve exactly the same process- returning a string constructed from an lvalue.