【发布时间】:2021-09-17 16:17:00
【问题描述】:
以下是代码:
#include <iostream>
const int& temp_func() {
return 3;
}
int main() {
std::cout << temp_func() << std::endl;
}
用g++编译时(Ubuntu 9.3.0-17ubuntu1~20.04),结果:
[1] 402809 segmentation fault ...
另一方面,当用clang++-12编译时,结果:
3
【问题讨论】:
-
如果你启用警告(你应该总是这样做,没有例外)你会得到一个很好的警告:
warning: returning reference to temporary [-Wreturn-local-addr]在 gcc 和 clang 中