2025
03
18
18
08
[C] 關於static_assert()的一些技巧
在zstd的source codes裡看到有趣的東西
/* static assert is triggered at compile time, leaving no runtime artefact.
* static assert only works with compile-time constants.
* Also, this variant can only be used inside a function. */
#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
這macro可以用來解決不支援static_assert(constant-expression)的c compiler.
#define STATIC_ASSERT(constant_expression) _Static_assert((constant_expression), "(" #constant_expression") failed")
這個則是可以簡化static_assert()的用法.