In the following snippet, no move and no copy of A
happens thanks to copy elision
struct A;
A function1();
A function2();
int main(int argc, char**) {
if (argc > 3) {
A a = function1();
} else {
A a = function2();
}
return 0;
}
This is nice, however a
is not accessible outside the if-block. When declaring a
outside, then a move happens
struct A;
A function1();
A function2();
int main(int argc, char**) {
A a;
if (argc > 3) {
a = function1();
} else {
a = function2();
}
return 0;
}
What is a recommendable attern to profit from copy elision when it should happen in an if block on the call site into a variable outside the if scope?