gpt4 book ai didi

c++ - 是否返回 void 有效代码?

转载 作者:太空狗 更新时间:2023-10-29 23:13:02 24 4
gpt4 key购买 nike

我发现以下代码被 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com