diff options
| author | dearblue <[email protected]> | 2021-10-24 23:11:52 +0900 |
|---|---|---|
| committer | dearblue <[email protected]> | 2021-10-24 23:11:52 +0900 |
| commit | b774832ee1a97c44cabbbaac004a3b784ed02a83 (patch) | |
| tree | 28e1cf6551e42b933d6a2fbeff1dce4970106e34 /include/mruby.h | |
| parent | 7850549a5edf2952056234203d2c72cf264f08eb (diff) | |
| download | mruby-b774832ee1a97c44cabbbaac004a3b784ed02a83.tar.gz mruby-b774832ee1a97c44cabbbaac004a3b784ed02a83.zip | |
Make `mrb_static_assert()` a variable argument
`mrb_static_assert()` extends the macro function to take one or two arguments.
If the argument is other than that, an error will occur.
References:
- static_assert のメッセージ省略を許可 - cpprefjp C++日本語リファレンス
https://cpprefjp.github.io/lang/cpp17/extending_static_assert.html
- c - Overloading Macro on Number of Arguments - Stack Overflow
https://stackoverflow.com/a/11763277
Diffstat (limited to 'include/mruby.h')
| -rw-r--r-- | include/mruby.h | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/include/mruby.h b/include/mruby.h index f80971543..99cd5fa2f 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -69,14 +69,18 @@ #define mrb_assert_int_fit(t1,n,t2,max) ((void)0) #endif -#if (defined __cplusplus && __cplusplus >= 201103L) || \ +#if (defined __cplusplus && __cplusplus >= 201703L) +# define mrb_static_assert(...) static_assert(__VA_ARGS__) +# define mrb_static_assert1(exp) static_assert(exp) +# define mrb_static_assert2(exp, str) static_assert(exp, str) +#elif (defined __cplusplus && __cplusplus >= 201103L) || \ (defined _MSC_VER) || \ (defined __GXX_EXPERIMENTAL_CXX0X__) /* for old G++/Clang++ */ -# define mrb_static_assert(exp, str) static_assert(exp, str) +# define mrb_static_assert2(exp, str) static_assert(exp, str) #elif defined __STDC_VERSION__ && \ ((__STDC_VERSION__ >= 201112L) || \ (defined __GNUC__ && __GNUC__ * 100 + __GNUC_MINOR__ >= 406)) -# define mrb_static_assert(exp, str) _Static_assert(exp, str) +# define mrb_static_assert2(exp, str) _Static_assert(exp, str) #else # /* alternative implementation of static_assert() */ # define _mrb_static_assert_cat0(a, b) a##b @@ -86,10 +90,24 @@ # else # define _mrb_static_assert_id(prefix) _mrb_static_assert_cat(prefix, __LINE__) # endif -# define mrb_static_assert(exp, str) \ +# define mrb_static_assert2(exp, str) \ struct _mrb_static_assert_id(_mrb_static_assert_) { char x[(exp) ? 1 : -1]; } #endif -#define mrb_static_assert1(exp) mrb_static_assert(exp, #exp) + +#ifndef mrb_static_assert +# define mrb_static_assert1(exp) mrb_static_assert2(exp, #exp) +# define mrb_static_assert_expand(...) __VA_ARGS__ /* for MSVC behaviour - https://stackoverflow.com/q/5530505 */ +# define mrb_static_assert_selector(a, b, name, ...) name +/** + * The `mrb_static_assert()` macro function takes one or two arguments. + * + * !!!c + * mrb_static_assert(expect_condition); + * mrb_static_assert(expect_condition, error_message); + */ +# define mrb_static_assert(...) \ + mrb_static_assert_expand(mrb_static_assert_selector(__VA_ARGS__, mrb_static_assert2, mrb_static_assert1)(__VA_ARGS__)) +#endif #include "mrbconf.h" |
