3 ms·
C[++] never ceases to amaze me. Just yesterday I saw this, totally valid, code snippet: void g(); void f() { return g(); }
by utopcell 2y ago
C[++] never ceases to amaze me. Just yesterday I saw this, totally valid, code snippet:
void g();
void f() {
return g();
}
- gpderetta 2y agoWhat's wrong with that?
- gizmo686 2y agoIn C function prototypes with no arguments are strange. void g(); Means that g is a function which takes a not-specified number of arguments (but not a variable number of arguments). Almost always what you want to do is void g(void) Which says that g takes 0 arguments. Having said that, declaring it as g() should work fine as long as you always provided that you always invoke it with the correct arguments. If you try invoking it with the wrong arguments, then the compiler will let you, and your program may just break in fun and exciting ways. Edit: looking closer, it looks like the intent might have been to alias f and g. But, as discussed above, it does so in a way that will break horribly if g expects any arguments.
- utopcell 2y agoWell, at the risk of stating the obvious: `return expr;` is meant to return whatever expr evaluates to. Here, g()'s return type is void, so there's no value to return and at the same time, f()'s return type is void, so return should not have an expr to begin with. This statement is effectively equivalent to: "g(); return;".
- deleted 2y ago[deleted]
- frizlab 2y agoso?
- josephg 2y ago> so return should not have an expr to begin with. Thats one way to think about it. Another is that void is a type - which is obviously true given you can have void* pointers and functions can return void. In this example, f() returns a void expression, so that’s a perfectly fine thing to return from g.
- utopcell 2y agovoid* is leaps-and-bounds different from void. It is an expression, it has storage, you can actually assign something to it. You can't have a variable of type void; "void var;" is meaningless. A function doesn't "return" void. void simply denotes that a function doesn't return anything.
- int_19h 2y agoYou can't have a variable of type void, but you can have an expression of type void - calling a function that returns void does just that (in C++, "throw" is also a void-typed expression).
- Narishma 2y agoNot the parent but I too don't see what's wrong with that.
- dzaima 2y agoThat's not valid standard C; gcc and clang give a warning with '-pedantic'. It's valid C++ though. And IMO it's quite a nice feature, useful sometimes for reducing boilerplate in early returns. It's the obvious consequence if you don't treat void as some extremely-special syntax but rather as just another type, perhaps alike an empty struct (though that's not valid C either ¯\_(ツ)_/¯) that's just implicitly returned at the end of a void-returning function, and a "return;" statement implicitly "creates" a value of void. In fact in Rust (and probably a bunch of other languages that I'm too lazy to remember) void-returning functions are done via returning a 0-tuple.
- utopcell 2y ago> warning: ISO C forbids 'return' with expression, in function returning void [-Wpedantic] Sanity restored.