是返回void有效code? [英] Is returning void valid code?

查看:133
本文介绍了是返回void有效code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现下面的code被采用Visual C ++ 2008接受和GCC编译器4.3:

I found out that the following code gets accepted by Visual C++ 2008 and GCC 4.3 compilers:

void foo()
{

}

void bar()
{
  return foo();
}

我有点惊讶,它编译。这是一个语言功能,或者是在编译器中的错误?什么的C / C ++标准,说这个?

I am a bit surprised that it compiles. Is this a language feature or is it a bug in the compilers? What do the C/C++ standards say about this?

推荐答案

这是的C语言特征++

It's a language feature of C++

C ++(ISO 14882:2003)6.6.3 / 3

C++ (ISO 14882:2003) 6.6.3/3

,类型为CV作废的前pression的return语句只能与CV无效的返回类型函数中使用;只是在函数返回到其调用者之前的前pression进行评估。

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

C (ISO 9899:1999) 6.8.6.4/1

和前pression return语句不得出现在它的返回类型的函数
  是无效的。

A return statement with an expression shall not appear in a function whose return type is void.

这篇关于是返回void有效code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆