Variably modified at file scope due to (void *)0 == (sem_t *)0?

Jonathan Wakely jwakely.gcc@gmail.com
Thu Oct 4 08:58:00 GMT 2018


On Thu, 4 Oct 2018 at 09:51, Sebastian Huber wrote:
>
> Hello,
>
> I use a typedef for static assertions for pre C11 compilers. For one
> static assertion
>
> typedef struct { int i; } sem_t;
>
> typedef int static_assert_sem_failed [((void *)0 == (sem_t *)0) ? 1 : -1];
>
> _Static_assert((void *)0 == (sem_t *)0, "sem_failed");
>
> I get a warning like this:
>
> gcc -S -o - sa.c > /dev/null
> sa.c:3:1: warning: variably modified ‘static_assert_sem_failed’ at file
> scope
>   typedef int static_assert_sem_failed [((void *)0 == (sem_t *)0) ? 1 : -1];
>   ^~~~~~~
>
> Is there some way to get around this warning?

You could move the homemade assertion so it's not at file scope:

__attribute__((used)) static void assertions() {
  __attribute__((used)) typedef int static_assert_sem_failed [((void
*)0 == (sem_t *)0) ? 1 : -1];
}

> The same expression in the
> _Static_assert() doen't lead to a warning.

It does with -pedantic, and that makes the reason for the warning clearer:

foo.c:5:26: warning: expression in static assertion is not an integer
constant expression [-Wpedantic]
 _Static_assert((void *)0 == (sem_t *)0, "sem_failed");
                ~~~~~~~~~~^~~~~~~~~~~~~



More information about the Gcc-help mailing list