在函数的返回类型末尾是否有空白的return语句是必需的吗? [英] Is a blank return statement at the end of a function whos return type is void necessary?

查看:55
本文介绍了在函数的返回类型末尾是否有空白的return语句是必需的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SO的大多数问题都涉及非无效返回类型,但与此同时我们正在进行一场火焰之战,并希望了解社区的想法.

Most of the questions on SO refer to non-void return types, but we are having a flame war at work about this and wanted to find out what the community thought.

void DoSomething()
{
   return; // Is this needed?
}

来自

From this discussion, it looks like the issue of having an undefined behavior deals with functions of non-void return types. Do void return types have this same undefined behavior, or is it only in the non-void returning function?

我担心的是,这最终将变成一种可怕的编码风格,没有任何根据.但是,如果这也是void返回函数的未定义行为,那么我认为有必要将其添加到编码标准中.如果C和C ++的答案不同,也可以.

My concern is that this will just end up as a terrible coding style that isn’t justified by anything. However if it’s also an undefined behavior for void return functions, then I can see the need for adding it to the coding standard. If the answer is different for C vs C++ this is ok too.

第6.6.3节return语句

§ 6.6.3 The return statement

2不带表达式的return语句只能在不返回值的函数,即具有返回void的类型,构造器(12.1)或析构函数(12.4).

2 A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type of void, a contrsuctor(12.1), or a destructor(12.4).

§6.6.3/2

流出一个函数的末尾是等同于没有价值的回报;这导致未定义的行为一个返回值的功能.

推荐答案

否;不需要.

如果您想提早返回而跳过其余功能,则只需编写 return; .

You only need to write return; if you want to return early and skip the rest of the function.

这篇关于在函数的返回类型末尾是否有空白的return语句是必需的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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