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 against something like the above, the Borland C ++ compiler issues a warning, MS VC ++ does not complain.

My question is: should the return from the void function be logically (theoretically) correct?
 return do_after_something(); 

Unlike:

 do_after_something(); return; 

or - does it all depend on the implementation (compiler / language)?

+5
source share
3 answers

Philosophically, you can say that you need to return the result of the void -returning function, but, unfortunately, this is not so, at least for Java.

This is valid for C ++. If you try 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 described in detail in ISO C++11 6.6.3 /3 :

The return statement with an expression of type void can only be used in functions with a return type of cv void ; an expression is evaluated immediately before the function returns to the caller.

Thus, it is truly fair to say that the Java path is correct if you think of void as not a valid type, but as the absence of something. For example, when you have:

 int xyzzy(void) { return 42; } 

in C, you are not required to specify an argument of the correct (not) type, for example:

 void plugh; int twisty = xyzzy(plugh); 

In addition, C ++ is also the right way, but in a different way, languages ​​are what they are.

+8
source

I think this 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 (for example, calling another void function), you are satisfied with this restriction.

It is not considered useful to do this in C (although I think it can be allowed) because there is no reason for this. Anytime:

 return someVoidFn(); 

You can always translate this:

 someVoidFn(); return; 

And get the same effect.

However, in C ++ and Java, returning the void function really makes sense, and that is because these languages ​​have templates and generics. Consider this (not very useful) Java class:

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

His run method returns the value that you pass to it after calling yourself a certain number of times. I know, pointless, right? But the important part is that T can be invalid. You can do it:

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

And it works. In Java, void (note capitalization) creates an instance of type void whose only value is null . If Java did not allow the return of the void expression in the void method, this should have been a compile-time error to create an instance using void.

+2
source

In C ++ 11 its possible and legal. You can return the void function from another void function. Refrence C ++ Programming Language Chapter 12, Bjarne Strousstrup

+1
source

Source: https://habr.com/ru/post/1207711/


All Articles