rcx

library of miscellaneous bits of C code
git clone git://git.rr3.xyz/rcx
Log | Files | Refs | README | LICENSE

commit 798ef8714182bff677eb386c979d3b782b072667
parent f8a5e7a7327914847422e78ca23d16526d2e59eb
Author: Robert Russell <robertrussell.72001@gmail.com>
Date:   Sun, 13 Aug 2023 12:53:40 -0700

Add static_assert macro

Diffstat:
Minc/def.h | 4++--
Minc/rcx.h | 8++++++--
2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/inc/def.h b/inc/def.h @@ -26,8 +26,8 @@ #define MAX(a,b) ((a) > (b) ? (a) : (b)) #define SWAP(a, b) do { \ - /* The check in the array length is basically a static_assert. The - * isize cast is needed, for otherwise -1 becomes unsigned. */ \ + /* The check in the array size is basically a portable static_assert. + * The isize cast is necessary; without it, -1 becomes unsigned. */ \ u8 r_swap_temp_[sizeof(a) == sizeof(b) ? (isize)sizeof(a) : -1]; \ memcpy(&r_swap_temp_, &(a), sizeof(a)); \ memcpy(&(a), &(b), sizeof(a)); \ diff --git a/inc/rcx.h b/inc/rcx.h @@ -1,7 +1,5 @@ #pragma once -/* TODO: consistency check: cast style: (void*) vs (void *) */ - #include <inttypes.h> #include <stddef.h> @@ -11,6 +9,12 @@ #if __STDC_VERSION__ >= 201100L #include <stdalign.h> #include <stdnoreturn.h> +/* We don't include assert.h, because we provide our own assert macro in + * debug.h, but we would still like C11's static_assert. In C23, static_assert + * becomes a keyword, so no define is necessary. */ +#if __STDC_VERSION__ < 202300L +#define static_assert _Static_assert +#endif #endif #include "def.h"