Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

void f()
{
   // some code here
   //
   return;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

Yes, you can return from a void function.

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

void foo()
{
  return void();
}

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();
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...