Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Interesting things in here.

  #define _(e) ({e;})
  //!< isolate expression e in its own lexical scope and clamp it with ;
  //!< note the outer parens, which is a very powerful c trick: they turn _(e) into a so called
  //!< r-value, which basically means we can do x=_(e) for as long as e evaluates to or returns
  //!< at least anything at all, i.e. not void. this macro is fundamental to k/simple implementation.
        
I didn't know that corner of C. Removing the () from the macro does change what you can pass as e, and assigning the result of a block does work as one would expect.

edit:

-Wpedantic on gcc will tell me ISO C doesn't like the construct but it still compiles it happily.

Clang offers -Wgnu-statement-expression-from-macro-expansion

So it looks likely that this is the GNU statement expression extension after all and not a part of C. Shame.



You can use these to implement Rust-style `auto x = TRY(...);` in C++ which is pretty nice. Unfortunately MSVC doesn't support this extension.


Yes, this is called statement-expression (instead of expression-statement which is the normal "doer" statement that contains just an expression followed by a semicolon).

The Linux kernel makes quite a bit of use of them as far as I'm aware.





Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: