Look at the following example:
string foo(int i) {
string a;
... Process i to build a ...
return a;
}
void bar(int j) {
const string& b = foo(j);
cout << b;
}
I know RVO and NRVO, but I thought that in order to do that, I need to write bar as the following:
void bar(int j) {
string b = foo(j);
cout << b;
}
Both versions seem to work, and I believe with the same performance. Is it safe to use the first version (with the const reference)?
Thanks.