From 45fb815a75669b32f6317ba6d53ec9465a4b0ae0 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Mon, 23 May 2022 11:53:22 +0200 Subject: [PATCH] util: implement STATIC_ASSERT using c++11 / c11 primitives Since we now require C11 and C++14, we can use the standard static_asserts from the standard library instead of rolling our own compiler-specific versions. To avoid needing scopes around usage in switch cases, keep the while-wrapping from before. This means it still can't be used outside of functions, but that should be fine; we should probably just use static_assert directly in those cases anyway. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Jesse Natalie Part-of: --- src/util/macros.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/src/util/macros.h b/src/util/macros.h index b48e10667ef..22b18303826 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -69,25 +69,9 @@ /** * Static (compile-time) assertion. */ -#if defined(_MSC_VER) - /* MSVC doesn't like VLA's, but it also dislikes zero length arrays - * (which gcc is happy with), so we have to define STATIC_ASSERT() - * slightly differently. - */ -# define STATIC_ASSERT(COND) do { \ - (void) sizeof(char [(COND) != 0]); \ - } while (0) -#elif defined(__GNUC__) - /* This version of STATIC_ASSERT() relies on VLAs. If COND is - * false/zero, the array size will be -1 and we'll get a compile - * error - */ -# define STATIC_ASSERT(COND) do { \ - (void) sizeof(char [1 - 2*!(COND)]); \ - } while (0) -#else -# define STATIC_ASSERT(COND) do { } while (0) -#endif +#define STATIC_ASSERT(cond) do { \ + static_assert(cond, #cond); \ +} while (0) /** * container_of - cast a member of a structure out to the containing structure