unreachable code is compile time error in languages like Java. But why it is just warning in C++ & C? Consider following example:
不可访问的代码是Java等语言中的编译时间错误。但是为什么它只是在c++和C语言中的警告呢?考虑下面的例子:
#include <iostream>
int f()
{
int a=3;
return a;
int b=6; // oops it is unreachable code
std::cout<<b; // program control never goes here
}
int main()
{
std::cout<<f()<<'\n';
}
Shouldn't compiler throw an error in this program, because statements after return statements in function f() will never get executed? What is the reason for allowing unreachable code?
编译器不应该在这个程序中抛出错误,因为函数f()中的返回语句之后将永远不会被执行?允许不可访问代码的原因是什么?
2 个解决方案
#1
12
Unreachable code is not a compile error in C++, but usually gives a warning, depending on your compiler and flags. If the compiler stopped when unreachable code is detected, then you would have less options for debugging code, because you would also have to manually remove code that is unnecessary.
在c++中,不可访问的代码不是编译错误,但通常会根据编译器和标志发出警告。如果在检测到不可访问代码时,编译器停止了,那么调试代码的选项就会减少,因为您还必须手动删除不必要的代码。
A warning instead of an error makes sense. It's good that it's mentioned as one could have unintentionally left old code behind, but there is no reason not to compile anyway.
警告而不是错误是有意义的。提到它很好,因为人们可能无意中忘记了旧代码,但无论如何,没有理由不进行编译。
#2
1
Unreachable code is a warning because there is no need for it to be an error, further, it can't always be easily avoided.
不可访问的代码是一个警告,因为不需要将它作为一个错误,而且,不能总是容易地避免它。
- Code expanded from macros or that check constants may result in unreachable code.
- 从宏扩展的代码或检查常量可能导致无法访问的代码。
- Code may reachable or not depending on the pre-processor defines
(common cross platform development for eg). - 根据预处理器定义(eg的通用跨平台开发),代码可能是可及的,也可能不是。
- Generated code may result in unreachable code that isn't practical to detect in the generation phase.
- 生成的代码可能会导致不可访问的代码,在生成阶段不实用。
Further, if you want this to be an error, GCC and CLANG support -Wunreachable-code
, so you can use -Werror=unreachable-code
此外,如果您希望这是一个错误,那么GCC和CLANG支持- wunreach -code,因此您可以使用-Werror=unreach -code
#1
12
Unreachable code is not a compile error in C++, but usually gives a warning, depending on your compiler and flags. If the compiler stopped when unreachable code is detected, then you would have less options for debugging code, because you would also have to manually remove code that is unnecessary.
在c++中,不可访问的代码不是编译错误,但通常会根据编译器和标志发出警告。如果在检测到不可访问代码时,编译器停止了,那么调试代码的选项就会减少,因为您还必须手动删除不必要的代码。
A warning instead of an error makes sense. It's good that it's mentioned as one could have unintentionally left old code behind, but there is no reason not to compile anyway.
警告而不是错误是有意义的。提到它很好,因为人们可能无意中忘记了旧代码,但无论如何,没有理由不进行编译。
#2
1
Unreachable code is a warning because there is no need for it to be an error, further, it can't always be easily avoided.
不可访问的代码是一个警告,因为不需要将它作为一个错误,而且,不能总是容易地避免它。
- Code expanded from macros or that check constants may result in unreachable code.
- 从宏扩展的代码或检查常量可能导致无法访问的代码。
- Code may reachable or not depending on the pre-processor defines
(common cross platform development for eg). - 根据预处理器定义(eg的通用跨平台开发),代码可能是可及的,也可能不是。
- Generated code may result in unreachable code that isn't practical to detect in the generation phase.
- 生成的代码可能会导致不可访问的代码,在生成阶段不实用。
Further, if you want this to be an error, GCC and CLANG support -Wunreachable-code
, so you can use -Werror=unreachable-code
此外,如果您希望这是一个错误,那么GCC和CLANG支持- wunreach -code,因此您可以使用-Werror=unreach -code