'Getting "_Static_assert is a C11-specific feature" with -std=c99 on FreeBSD

Reduced to simplest reproducible size:

#include <assert.h>

#ifdef static_assert
static_assert(1 == 1, "oops!");
#endif

compiled with -std=c99 -pedantic gives warning: _Static_assert is a C11-specific feature [-Wc11-extensions] on FreeBSD (tested on 11.2 and 12.3, which use clang 6.0 and 10.0 respectively).

I don't get this problem on Linux (static_assert does not get defined when std=c99 is specified) with either gcc or clang (various versions).

The #ifdef guard is specifically to avoid the problem of trying to use a C11 feature when it's not available!

(As I said, this is the smallest reproducible size. Actually I want to define my own static_assert if there's not one available, and have the source code compile cleanly on as many systems as possible.)

On FreeBSD, assert.h defines static_assert if __ISO_C_VISIBLE >= 2011. __ISO_C_VISIBLE is being set by default in sys/cdefs.h.

On Linux, assert.h defines static_assert if __USE_ISOC11 is defined, and __USE_ISOC11 is defined by features.h if _ISOC11_SOURCE is defined.

I'm somewhat surprised that __STDC_VERSION__ is not being checked on FreeBSD (the Linux version will define static_assert if __STDC_VERSION__ >= 201112L). But on both I'm somewhat surprised that a lower value of __STDC_VERSION__ isn't being used to restrict the feature.

I can work around this with some FreeBSD-specific checks, but is FreeBSD doing it "wrong" or is Linux doing it "wrong"?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source