因为我来自一个 java 岛,所以我很奇怪为什么编译器不会像这样警告无法访问的代码:
int main(int argc, char** argV)
{
std::list<int> lst = {1,2,3,4};
return 0;
std::cout << "Done!!!" << std::endl;
return 0;
}
我的问题:
为什么我可以编译出有 2 个返回值的代码?
我的编译器是 c++11 的 gcc,在 Windows 上,代码块
最佳答案
I wounder why the compiler doesnt warns about unreachable code in something like
在 gcc documentaion 中有很好的解释关于警告:
-Wunreachable-code
Warn if the compiler detects that code will never be executed. This option is intended to warn when the compiler detects that at least a whole line of source code will never be executed, because some condition is never satisfied or because it is after a procedure that never returns.
It is possible for this option to produce a warning even though there are circumstances under which part of the affected line can be executed, so care should be taken when removing apparently-unreachable code.
For instance, when a function is inlined, a warning may mean that the line is unreachable in only one inlined copy of the function.
This option is not made part of -Wall because in a debugging version of a program there is often substantial code which checks correct functioning of the program and is, hopefully, unreachable because the program does work. Another common use of unreachable code is to provide behavior which is selectable at compile-time.
虽然 g++ 5.1.0 不会为此代码生成任何警告,即使启用了此选项也是如此。
关于c++ - 为什么我可以编译有 2 个返回的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372132/