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

  /*
   * This returns a constant expressionn while determining if an argument is
   * a constant expression, most importantly without evaluating the argument.
   * Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>
 */
  #define __is_constexpr(x) \
          (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))


Some explanation from Linus about how this trick works: https://lkml.org/lkml/2018/3/20/845


"sizeof((void )1)"

"- this will break the minds of everybody who ever sees that expression."

It indeed broke my mind!


I am sure you meant

  sizeof((void*)1)


Given the italics, it wouldn’t surprise me if the original was in fact:

sizeof(*(void*)1)


The dark side of c is a pathway to many abilities, some considered to be unnatural


Jesus, can someone unravel that for me?


I can try... In short, this is about how a C compiler is supposed to deduce the type of the ternary operation. So, if x is a constant expression, the compiler figures that the second operand must be NULL (because it is allowed to perform the multiplication, by 0l), whose type then is whatever the third operand says (a pointer to int). The comparison succeeds. Otherwise the compiler does not perform the multiplication, takes the type of the second operand at its “face value”, i.e. as a pointer to void, and converts the type of the third operand to it - which, of course, makes the comparison false in this case.


I found a detailed explanation here:

https://stackoverflow.com/a/49481218


Wow! the above code is described by itself about what Torvalds means with: very odd header files that *pushes the boundary* of what can be called "C".

I'm not a C dev BTW.


wow! As mainly a Java engineer. This is an alien language to me.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

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

Search: