variadic-macros

How to write a macro in Rust to match any element in a set?

拟墨画扇 提交于 2020-01-17 04:58:08
问题 In C, I'm used to having: if (ELEM(value, a, b, c)) { ... } which is a macro with a variable number of arguments to avoid typing out if (value == a || value == b || value == c) { ... } A C example can be seen in Varargs `ELEM` macro for use with C. Is this possible in Rust? I assume it would use match . If so, how would variadic arguments be used to achieve this? 回答1: macro_rules! cmp { // Hack for Rust v1.11 and prior. (@as_expr $e:expr) => { $e }; ($lhs:expr, $cmp:tt any $($rhss:expr),*) =>

C-preprocessor: iteratively expand macro to comma-separated list

别说谁变了你拦得住时间么 提交于 2020-01-13 10:44:11
问题 Using Paul Fultz II's solution in the post C-preprocessor recursive macro, I'd like to expand an unlimited number of parenthesized macro arguments, e.g. #define MY_CHAIN (alpha) (beta) (gamma) into a comma-separated list which can be passed to a variadic macro, e.g. CHAIN_COMMA(MY_CHAIN) // alpha, beta, gamma I'm able to expand into braces [alpha] [beta] [gamma] and delimit the list with everything I've tried except a comma, alpha :: beta :: gamma in the example below. Here is my full

A #define in C with three dots

二次信任 提交于 2020-01-09 10:06:50
问题 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code LOGI and LOGW are used this way LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); and this way LOGW("Unable to eglMakeCurrent"); Since I try to avoid complex macros and #define in general, I

A #define in C with three dots

99封情书 提交于 2020-01-09 10:06:34
问题 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code LOGI and LOGW are used this way LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z); and this way LOGW("Unable to eglMakeCurrent"); Since I try to avoid complex macros and #define in general, I

Case variadic macro in C

拈花ヽ惹草 提交于 2020-01-05 05:18:50
问题 I have 2 wrapper macros for asserting function input parameters: /** * @brief An assert wrapper with no value return in case assert fails. * @param x_: value to test for being non zero. */ #define UTIL_ASSERT_VOID(x_) \ assert_param(x_); \ if (!x_) \ return; \ /** * @brief An assert wrapper with a value return in case assert fails. * @param x_: value to test for being non zero. */ #define UTIL_ASSERT_VAL(x_, ret_) \ assert_param(x_); \ if (!x_) \ return ret_; \ The former is used in functions

Variadic macros: expansion of pasted tokens

♀尐吖头ヾ 提交于 2020-01-03 18:31:09
问题 I'm wondering if it's possible to "nest" variadic macro invocations. I'm only truly concerned with GCC and Clang. My macro definition looks like this: /** * @brief Invoke an instance method. */ #define $(obj, method, ...) \ ({ \ typeof(obj) _obj = obj; \ _obj->interface->method(_obj, ## __VA_ARGS__); \ }) I use this to conveniently call "instance methods" in my OO framework (https://github.com/jdolan/objectively): $(array, addObject, obj); Works boss. Unfortunately, I haven't yet figured out

Is the `gnu-zero-variadic-macro-arguments` safe to ignore?

僤鯓⒐⒋嵵緔 提交于 2020-01-02 10:23:22
问题 Consider the following code (live example): #define TEST_VA(mX, ...) TEST #define STRINGIFY_IMPL(mX) #mX #define STRINGIFY(mX) STRINGIFY_IMPL(mX) #include <iostream> int main() { std::cout << STRINGIFY(TEST_VA(1)) << std::endl; std::cout << STRINGIFY(TEST_VA()) << std::endl; return 0; } clang++ 3.4 complains: main.cpp:9:37: warning: must specify at least one argument for '...' parameter of variadic macro [-Wgnu-zero-variadic-macro-arguments] std::cout << STRINGIFY(TEST_VA(1)) << std::endl; ^

Overloading a macro

家住魔仙堡 提交于 2020-01-01 17:07:30
问题 I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea

Overloading a macro

我的未来我决定 提交于 2020-01-01 17:07:05
问题 I'm trying to overload a macro by the number of parameter. Of course I can't actually overload the macro. I've tried using variadic macros to choose the right macro (using the fact that if __VA_ARGS__ doesn't exist it's supposed to delete the last coma before it - GCC Reference ): #define TEST1() printf("TEST1"); #define TEST2() printf("TEST2"); #define CHOOSER(x, y,FUNC,...) FUNC() #define MANIMACRO(...) CHOOSER(,__VA_ARGS__,TEST1,TEST2) int main(void) { MANIMACRO(1); MANIMACRO(); } The idea

Variadac Macro apply macro to all arguments

偶尔善良 提交于 2019-12-23 19:43:30
问题 I was experimenting with C++11 variadac macros. I was trying to apply another macro to each argument in the list. This is my first try: #define APPLY_CHAIN(first, ...) APPLY_ACT(first) APPLY_CHAIN( __VA_ARGS__ ) Unfortunately this did not work. I eventually got it to work. But it was a bit convoluted and has a limit of 'n' (where 'n' is a max size that I am willing to type macros for). #define COUNT_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N #define COUNT(...) COUNT_N( __VA_ARGS__,