我可以返回void函数吗? [英] Can I return in void function?

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

问题描述

我必须返回上一级的递归。是下面的语法吗?

I have to return to the previous level of the recursion. is the syntax like below right?

void f()
{
   // some code here
   //
   return;
}


推荐答案

一个void函数。

有趣的是,你也可以从void函数返回void。例如:

Interestingly, you can also return void from a void function. For example:

void foo()
{
  return void();
}

正如预期,这与一个简单的 return; 。它可能看起来很神秘,但原因是模板一致性:

As expected, this is the same as a plain return;. It may seem esoteric, but the reason is for template consistency:

template<class T>
T default_value()
{
  return T();
}



这里, default_value 返回一个类型为T的默认构造的对象,并且由于返回 void 的能力,即使 T = void

Here, default_value returns a default-constructed object of type T, and because of the ability to return void, it works even when T = void.

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

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