I wrote this code and compile with gcc. I expected to get result "2", but result was "0".
Other compiler clang and vc prints "2". Is it undefined behaviour or not?
#include <stdio.h>
struct Test {
Test& inc() {
++value;
return *this;
}
int value = 1;
};
int main() {
auto&& t = Test().inc(); // The life-time of the temporary might extended.
printf("%d\n", t.value); // gcc prints "0". dangling reference?
return 0;
}
c.f. build reslut on http://rextester.com
auto&& t = Test().inc()
is notauto&& t = Test(); t.inc();
. Thank you @Quentin ! - sumomonekogcc-7 -fsanitize-address-use-after-scope
can detect this err. - sumomoneko