|
|
Log in / Subscribe / Register

Void is surprisingly useful.

Void is surprisingly useful.

Posted Sep 9, 2026 14:52 UTC (Wed) by alx.manpages (subscriber, #145117)
In reply to: Void is surprisingly useful. by iabervon
Parent article: Stabilizing Rust's never type

"void (*)(int)" is "function that takes int and calling it is a statement rather than an expression"

void functions still produce expressions. This can be seen in the comma operator:

void f(void);

42, f(), 42;

exit() returns void, but it's still usable as an expression. You just can't assign it to something, because variables of type void are not allowed. But that doesn't make them statements.


to post comments

Void is surprisingly useful.

Posted Sep 9, 2026 16:17 UTC (Wed) by NYKevin (subscriber, #129325) [Link]

I think a better way of expressing it might be "void must be discarded."

(I was going to say "void can't be an rvalue," but your example is indeed an rvalue!)

Void is surprisingly useful.

Posted Sep 13, 2026 9:35 UTC (Sun) by koflerdavid (subscriber, #176408) [Link] (1 responses)

Thanks for showing this, I didn't know this compiles.

Void is surprisingly useful.

Posted Sep 13, 2026 11:31 UTC (Sun) by alx.manpages (subscriber, #145117) [Link]

Another --more interesting-- example is with the ternary operator:

#define assert(e)  ((e) ? (void)0 : abort())


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds