简体   繁体   English

从void函数返回

[英]Return from void function

Suppose a class exists as follows: 假设一个类存在如下:

class Foo
{
    void do_after_something()
    {
    //some code here
    return;
    }

    void do_something()
    {
    //some code here
    return do_after_something(); //returning another (void) function
    }
};

JAVA is clearly opposed to something like the above, Borland C++ compiler issues a warning, MS VC++ does not complain. JAVA明显反对上面这样的东西,Borland C ++编译器发出警告,MS VC ++不抱怨。

My question is: Should returning from a void function be logically (theoretically) correct? 我的问题是:从void函数返回逻辑上(理论上)是否正确?

 return do_after_something(); 

as opposed to: 而不是:

 do_after_something(); return; 

or is it all implementation (compiler/language) dependent? 或者是所有实现(编译器/语言)依赖?

Philosophically, you could argue that returning the result of a void -returning function should be allowed but, sadly, that's not the case here, at least for Java. 从哲学上讲,你可能会认为应该允许返回void -returning函数的结果,但遗憾的是,这不是这里的情况,至少对于Java而言。

It is valid for C++ however. 有效的C ++但是。 If you try out the following program: 如果您尝试以下程序:

#include <iostream>

void xyzzy(void) {}
void plugh(void) { return xyzzy();}

int main() {
    std::cout << "Hello\n";
    plugh();
    return 0;
}

it will work fine. 它会工作正常。

This is detailed in ISO C++11 6.6.3 /3 : 这在ISO C++11 6.6.3 /3有详细说明:

A return statement with an expression of type void can be used only in functions with a return type of cv void ; 表达式为void return语句只能在返回类型为cv void函数中使用; the expression is evaluated just before the function returns to its caller. 在函数返回其调用者之前计算表达式。

So it's really equally valid to argue that the Java way is correct if you think of void as not an actual type, but as an absence of something. 因此,如果您认为void不是实际类型,而是缺少某些东西,那么认为Java方法是正确的,这同样有效。 For example, when you have: 例如,当你有:

int xyzzy(void) { return 42; }

in C, you're not forced to provide an argument of the correct (non-)type, such as with: 在C中,您不必强制提供正确(非)类型的参数,例如:

void plugh;
int twisty = xyzzy(plugh);

Ditto, the C++ way is correct as well, but in a different way - the languages are what they are. 同上,C ++方式也是正确的,但以不同的方式 - 语言就是它们。

I think it should be allowed and considered valid in all languages. 我认为它应该被允许并被认为在所有语言中都有效。 If you have a function whose return type is void and you return the result of an expression whose type is void (such as calling another void function), you've satisfied that constraint. 如果您有它的返回类型是功能void ,并返回其类型是一个表达式的结果void (如调用另一个void功能),你纳约束。

It's not considered useful to do this in C (though I think it may be allowed) because there's no reason to need it. 在C语言中这样做是不合适的(虽然我认为可能允许),因为没有理由需要它。 Anytime you do: 你做的任何时候:

return someVoidFn();

You can always translate that to: 您始终可以将其翻译为:

someVoidFn();
return;

And get the exact same effect. 并获得完全相同的效果。

However, in C++ and Java, returning a void function does have a real use, and that's because those languages have templates and generics . 但是,在C ++和Java中,返回void函数确实有用,那是因为这些语言有模板和泛型 Consider this (not very useful) Java class: 考虑这个(不是很有用)Java类:

class NestedIdentity<T> {
  T run(int i, T value) {
    if (i == 0) return value;
    return run(i - 1);
  }
}

Its run method returns the value you pass to it, after calling itself a given number of times. 它的run方法在调用给定次数后返回传递给它的值。 I know, pointless, right? 我知道,没有意义,对吧? But the important part is that T may be void . 但重要的是T 可能是无效的 You can do this: 你可以这样做:

NestedIdentity<Void> nest = new NestedIdentity<Void>();
nest(5, null);

And this works . 这很有效 In Java, Void (note the capitalization) instantiates a generic with a void -like type whose only value is null . 在Java中, Void (注意大小写)实例化一个类似于void的类型的泛型,其唯一值为null If Java didn't allow returning a void expression in a void method, it would have to be a compile-time error to instantiate a generic with void. 如果Java不允许在void方法中返回void表达式,那么使用void实例化泛型则必须是编译时错误。

In C++ 11 its possible and legal . 在C ++ 11中,它可能是合法的。 You can return a void function from other void function. 您可以从其他void函数返回void函数。 Refrence The C++ Programming Language Chapter 12 , Bjarne Strousstrup 参考C ++编程语言第12章,Bjarne Strousstrup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM