c++ - 为什么我可以编译有 2 个返回的代码?

标签 c++ codeblocks compiler-warnings unreachable-code

因为我来自一个 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/

相关文章:

c++ - 如何将字符串转换为 uint32_t

C++ fwrite,错误代码 36 Broken Pipe

C: 使一个类型与任何其他类型不兼容

c - c 中的 & 符号错误和生命周期

compilation - 由于语法错误,Clojure 后置条件无法执行——为什么?

C++ : Is it possible to write Mock class for a class with parameterized constructor

c++ - 尝试在具有 GMP 的 C++ 上使用 RSA 破译文件会产生不可预测的结果

c++ - 从接口(interface)和实现 C++ 继承

c++ - Codeblocks Windows 和 makefile 生成 C++

c++ - 如何隐藏wxWidgets wxGrid索引