Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>NEW ANSWER </strong>:</p> <p>In my original answer (below), I had to have two different macros to support assertions in a function scope and at the global scope. I wondered if it was possible to come up with a single solution that would work in both scopes.</p> <p>I was able to find a solution that worked for Visual Studio and Comeau compilers using extern character arrays. But I was able to find a more complex solution that works for GCC. But GCC's solution doesn't work for Visual Studio. :( But adding a '#ifdef __ GNUC __', it's easy to choose the right set of macros for a given compiler.</p> <p><strong>Solution:</strong></p> <pre><code>#ifdef __GNUC__ #define STATIC_ASSERT_HELPER(expr, msg) \ (!!sizeof \ (struct { unsigned int STATIC_ASSERTION__##msg: (expr) ? 1 : -1; })) #define STATIC_ASSERT(expr, msg) \ extern int (*assert_function__(void)) [STATIC_ASSERT_HELPER(expr, msg)] #else #define STATIC_ASSERT(expr, msg) \ extern char STATIC_ASSERTION__##msg[1]; \ extern char STATIC_ASSERTION__##msg[(expr)?1:2] #endif /* #ifdef __GNUC__ */ </code></pre> <p>Here are the error messages reported for <code>STATIC_ASSERT(1==1, test_message);</code> at line 22 of test.c:</p> <p><strong>GCC:</strong></p> <pre><code>line 22: error: negative width in bit-field `STATIC_ASSERTION__test_message' </code></pre> <p><strong>Visual Studio:</strong></p> <pre><code>test.c(22) : error C2369: 'STATIC_ASSERTION__test_message' : redefinition; different subscripts test.c(22) : see declaration of 'STATIC_ASSERTION__test_message' </code></pre> <p><strong>Comeau:</strong></p> <pre><code>line 22: error: declaration is incompatible with "char STATIC_ASSERTION__test_message[1]" (declared at line 22) </code></pre> <p><p>&nbsp;<br>&nbsp;<br></p> <p><strong>ORIGINAL ANSWER </strong>:</p> <p>I do something very similar to what Checkers does. But I include a message that'll show up in many compilers:</p> <pre><code>#define STATIC_ASSERT(expr, msg) \ { \ char STATIC_ASSERTION__##msg[(expr)?1:-1]; \ (void)STATIC_ASSERTION__##msg[0]; \ } </code></pre> <p>And for doing something at the global scope (outside a function) use this:</p> <pre><code>#define GLOBAL_STATIC_ASSERT(expr, msg) \ extern char STATIC_ASSERTION__##msg[1]; \ extern char STATIC_ASSERTION__##msg[(expr)?1:2] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload