

“:-!!” what is it in c code? - Arkid
http://stackoverflow.com/questions/9229601/what-is-it-in-c-code

======
xd
The actual line is _#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e);
}))_

The guy asking the question just misread the code, the colon is part of the
struct definition and the key part is -!! which coerces an integer to 0 if the
value is zero, or -1 if the value is anything other than 0.

~~~
ajross
They're all key parts. Note that the sizeof is there to avoid polluting the
namespace. Otherwise you'd have to declare something that used that anonymous
struct. And likewise the parentheses so it can be used in all contexts in an
expression. Every byte of that macro is needed. It's pretty elegant, honestly.

~~~
nash
The sizeof is there so it returns a value so it can be used as an expression.
See the comment above the code (reproduced in the original question).

------
apaprocki
These machinations were always necessary to get compile-time asserts. Luckily
C++11 and C11 are both providing static_assert so there will be _real_
compiler support instead of having to trick it into dumping an error.

edit: Another common one is using __LINE__ or __COUNTER__ in an array symbol
name and having the test value result in an array size of -1.

------
zokier
Reminds me of the 'goes to' operator:

    
    
      for (int i = 42; i --> 0;) { foo(); }

~~~
eridius
That's really cute, but it's also buggy. The loop will start at i == 41.

~~~
jacquesm
You are right about it starting on 41, but chances are that that is its
intended behavior, with a bit of context that is more clear:

    
    
      #define ARRAYSIZE 42
    
      int array[ARRAYSIZE];
    
      void f() {
         for (i=ARRAYSIZE; i --> 0;) {
           printf("%d\n", array[i]);
         }
      }
    

If it were to start at 42 you'd be referencing the array outside of its
bounds.

------
kyberias
This has been posted before during the past 2 months.

~~~
ward
For those wondering <http://news.ycombinator.com/item?id=3577205> (29 days
ago)

Didn't get much attention back then. (+8 at time of writing)

~~~
smsm42
I know I didn't notice it back then but did notice it now (and it's a neat
trick which I didn't encounter before), so for me reposting worked.

