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

标签 c++ c

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

相关文章:

java - 为 Windows Mobile 6.1 选项编写应用程序?

c++ - std::deque 什么时候需要重新分配?

c++ - 关于一个编译器同时执行多个任务

c - 从套接字读取字符串时,不打印第一个字符

c - 开关案例分配,我的案例都没有提供输出

c - Rust 字符串和 C 可变参数函数

c - 这是什么意思,为什么句子里有(int)?

c++ - 从 .txt 文件加载数组索引 - 分隔符

c++ - C++ 调用约定是否受标准约束,因为函数的返回类型不需要在声明 fn 时定义?

c++ - C 或 C++ 或 WinApi 中是否有任何函数可以创建目录,包括指定路径中所有不存在的目录?