spa: support single argument static assertions in older standards

Single argument static_assert() is only available since
C++17 and C23. Some compilers accept it even in earlier language
versions, but some do not.

Single argument static assertions can be supported by using
a GCC extensions, namely that `, ## __VA_ARGS__` removes the
comma if the variadic argument list is empty. This enables a
construction which passes a pre-determined string as the second
argument of the underlying static_assert() when only a single
argument is used in the `SPA_STATIC_ASSERT()` macro.

Fixes #3050
This commit is contained in:
Barnabás Pőcze 2023-03-03 19:27:23 +01:00 committed by Wim Taymans
parent 57a89e5723
commit 39ce32985b

View file

@ -8,18 +8,21 @@
#ifdef __cplusplus
extern "C" {
# if __cplusplus >= 201103L
# define SPA_STATIC_ASSERT static_assert
# define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) static_assert(expr, msg)
# endif
#else
# include <stdbool.h>
# if __STDC_VERSION__ >= 201112L
# define SPA_STATIC_ASSERT _Static_assert
# define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) _Static_assert(expr, msg)
# endif
#endif
#ifndef SPA_STATIC_ASSERT
#define SPA_STATIC_ASSERT(a, b) \
((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(a) - 1; }))
#ifndef SPA_STATIC_ASSERT_IMPL
#define SPA_STATIC_ASSERT_IMPL(expr, ...) \
((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(expr) - 1; }))
#endif
#define SPA_STATIC_ASSERT(expr, ...) SPA_STATIC_ASSERT_IMPL(expr, ## __VA_ARGS__, "`" #expr "` evaluated to false")
#include <inttypes.h>
#include <signal.h>
#include <stdlib.h>