https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85182

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
The GCC feature that comes close to what you're looking for from _Static_assert
are attributes error and warning.  The difference is that the attributes are
considered very late during translation while _Static_assert is evaluated very
early, during parsing.

Here's an example of how to set it up:

$ cat static-assert.[hc] && gcc -O2 -S -Wall static-assert.c
#include "static-assert.h"

void f (void) { bar (1); }
void g (void) { bar (0); }
void h (int i) { bar (i); }

void __attribute__ ((error ("my static assertion failed")))
my_static_assert_fail (void);

void __attribute__ ((warning ("my static assertion unable to evaluate
expression")))
my_static_assert_nonconst (void);

static void inline __attribute__ ((always_inline))
my_static_assert (int expr)
{
  if (!__builtin_constant_p (expr))
    {
      my_static_assert_nonconst ();
      return;
    }

  if (expr)
    return;

  my_static_assert_fail ();
}

static inline void bar (int i)
{
  my_static_assert (i);
}
In file included from static-assert.c:1:
In function ‘my_static_assert’,
    inlined from ‘g’ at static-assert.h:24:3:
static-assert.h:19:3: error: call to ‘my_static_assert_fail’ declared with
attribute error: my static assertion failed
   my_static_assert_fail ();
   ^~~~~~~~~~~~~~~~~~~~~~~~
In function ‘my_static_assert’,
    inlined from ‘h’ at static-assert.h:24:3:
static-assert.h:12:7: warning: call to ‘my_static_assert_nonconst’ declared
with attribute warning: my static assertion unable to evaluate expression
       my_static_assert_nonconst ();
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reply via email to