我发现以下代码被 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++ - 是否返回 void 有效代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42254156/