我发现以下代码被 Visual C++ 2008 和 GCC 4.3 编译器接受:
void foo()
{
}
void bar()
{
return foo();
}
我有点惊讶它可以编译。这是语言功能还是编译器中的错误? C/C++ 标准对此有何规定?
最佳答案
这是 C++ 的语言特性
C++ (ISO 14882:2003) 6.6.3/3
A return statement with an expression of type “cv void” can be used only in functions with a return type of cv void; the expression is evaluated just before the function returns to its caller.
C (ISO 9899:1999) 6.8.6.4/1
A return statement with an expression shall not appear in a function whose return type is void.
关于c++ - 是否返回无效的有效代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3383090/