#include <iostream>
#include <utility>
#include <string>
int anotherFunc() {
return 42;
}
int my_func() {
std::string s; // 只要这里构造了std::string,后续std::unreachable()后的函数调用就会编译失败
if (rand() % 2 == 0)
return {};
std::unreachable();
anotherFunc(); // 只要上面有std::string,这行在GCC里就过不了编译
}
int main(int argc, char* argv[]) {
my_func();
}
编译时会触发如下错误:
: In function 'int my_func()':
:24:1: error: control reaches end of non-void function [-Werror=return-type]
24 | }
| ^
cc1plus: some warnings being treated as errors