From 39ce32985bb0dad4f9d9da5f1c6dcd943baa09dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Fri, 3 Mar 2023 19:27:23 +0100 Subject: [PATCH] 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 --- spa/include/spa/utils/defs.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/spa/include/spa/utils/defs.h b/spa/include/spa/utils/defs.h index a52d00128..8940b52df 100644 --- a/spa/include/spa/utils/defs.h +++ b/spa/include/spa/utils/defs.h @@ -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 # 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 #include #include