0
我写了这段代码并用gcc编译。 我希望得到结果“2”,但结果是“0”。通用引用和命名参数Ideom
其他编译器clang和vc打印“2”。 它是未定义的行为还是不行?
#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.建立reslut上http://rextester.com
http://rextester.com/GBM44684 – sumomoneko
我误解了自动演绎。 'auto && t = Test().inc()'不是'auto && t = Test(); t.inc();'。谢谢@Quentin! – sumomoneko
'gcc-7 -sanitize-address-use-after-scope'可以检测到这个错误。 – sumomoneko