I tried using lambda to conditionally link a reference to one of two variables:
int foo, bar;
int &choice = [&]() -> int & {
if (true /* some condition */) {
return foo;
} else {
return bar;
}
}();
This gives a warning in clang 3.4:
stack_stuffing.cpp:5:20: warning: reference to stack memory associated with
local variable 'foo' returned [-Wreturn-stack-address]
return foo;
^~~
stack_stuffing.cpp:7:20: warning: reference to stack memory associated with
local variable 'bar' returned [-Wreturn-stack-address]
return bar;
^~~
But I only return links to the memory stack, which are in the area where the lambda is called. Is this behavior indicated, unspecified, or a clang error?
source
share