简体   繁体   English

C ++和预处理器宏:可变参数类型

[英]C++ and preprocessor macros: variadic type

Take for example the following macro: 以下面的宏为例:

#define _CREATE_VAR(X1) double X1{smc::define_variable (data, X1, #X1)};
#define _CREATE_VAR2(X1,X2) double X1{smc::define_variable (data, X1, #X1)}; /
                        double X2{smc::define_variable (data, X2, #X2)}; /
#define _CREATE_VAR3(X1,X2,X3) double X1{smc::define_variable (data, X1, #X1)}; /
                        double X2{smc::define_variable (data, X2, #X2)}; /
                        double X3{smc::define_variable (data, X3, #X3)}; /
#define _CREATE_VAR4(X1,X2,X3,X4) double X1{smc::define_variable (data, X1, #X1)}; /
                        double X2{smc::define_variable (data, X2, #X2)}; /
                        double X3{smc::define_variable (data, X3, #X3)}; /
                        double X4{smc::define_variable (data, X4, #X4)}; /
#define _CREATE_VAR5(X1,X2,X3,X4,X5) double X1{smc::define_variable (data, X1, #X1)}; /
                        double X2{smc::define_variable (data, X2, #X2)}; /
                        double X3{smc::define_variable (data, X3, #X3)}; /
                        double X4{smc::define_variable (data, X4, #X4)}; /
                        double X5{smc::define_variable (data, X5, #X5)}; /

Is there a way to simplify this by using one macro _CREATE_VAR instead of having multiple instances with different variable argumentsand corresponding names? 有没有一种方法可以通过使用一个宏_CREATE_VAR而不是让多个实例具有不同的变量参数和相应的名称来简化此过程? Ideally, I would like to automatically call the same macro _CREATE_VAR regardless of how many arguments. 理想情况下,无论有多少个参数,我都希望自动调用同一宏_CREATE_VAR

If you don't mind a slightly different call syntax, you can use boost.preprocessor for that: 如果您不介意调用语法略有不同,则可以使用boost.preprocessor

#include "boost/preprocessor.hpp"

// or to not include entire preprocessor header, the following header files will do
// #include <boost/preprocessor/stringize.hpp>
// #include <boost/preprocessor/seq/for_each.hpp>

#define CREATE_ONE_VAR(maR_, maData_, maVarName) \
  double maVarName {smc::define_variable (data, maVarName, BOOST_PP_STRINGIZE(maVarName))};

#define CREATE_VAR(maSeq) \
  BOOST_PP_SEQ_FOR_EACH(CREATE_ONE_VAR, %%, maSeq)

Example of use: 使用示例:

CREATE_VAR((x1)(x2)(x3))  //does the same as your original _CREATE_VAR3(x1, x2, x3)

Now you can call it with any number of variables from 1 to BOOST_PP_LIMIT_SEQ , which is normally 256. 现在,您可以使用从1到BOOST_PP_LIMIT_SEQ任意数量的变量来调用它,通常为256。

A few notes: I use %% to indicate that the argument is unused. 一些注意事项:我使用%%表示该参数未使用。 You can put anything in there (it gets passed to the internal macro's maData parameter, which we don't use). 您可以在其中放置任何内容(将其传递给内部宏的maData参数,我们不使用它)。

You should not name your macros to start with an underscore followed by a capital letter. 您不应将宏命名为以下划线和大写字母开头。 It's illegal according to the standard, as such symbols (as well as any symbol including two consecutive underscores) are reserved for your compiler. 根据标准,这是非法的,因为此类符号(以及包括两个连续下划线的任何符号)都保留给编译器使用。

One way to do it is to use a FOR_EACH macro on __VA_ARGS__ , its not pretty and it probably takes a while to fully follow along with whats happening but at least it doesn't depend on boost . 做到这一点的方法之一是使用FOR_EACH宏观__VA_ARGS__ ,它不漂亮,它可能需要一段时间才能完全伴随着发生了什么,但至少它不依赖于跟随boost

Helper macros: 辅助宏:

// Concatenates tokens, even when the tokens are macros themselves.
#define PP_JOIN_HELPER_HELPER(_0, _1)       _0##_1
#define PP_JOIN_HELPER(_0, _1)              PP_JOIN_HELPER_HELPER(_0, _1)
#define PP_JOIN_IMPL(_0, _1)                PP_JOIN_HELPER(_0, _1)

#define PP_JOIN_2(_0, _1)                                                                   PP_JOIN_IMPL(_0, _1)
#define PP_JOIN_3(_0, _1, _2)                                                               PP_JOIN_2(PP_JOIN_2(_0, _1), _2)
#define PP_JOIN_4(_0, _1, _2, _3)                                                           PP_JOIN_2(PP_JOIN_3(_0, _1, _2), _3)
#define PP_JOIN_5(_0, _1, _2, _3, _4)                                                       PP_JOIN_2(PP_JOIN_4(_0, _1, _2, _3), _4)
#define PP_JOIN_6(_0, _1, _2, _3, _4, _5)                                                   PP_JOIN_2(PP_JOIN_5(_0, _1, _2, _3, _4), _5)
#define PP_JOIN_7(_0, _1, _2, _3, _4, _5, _6)                                               PP_JOIN_2(PP_JOIN_6(_0, _1, _2, _3, _4, _5), _6)
#define PP_JOIN_8(_0, _1, _2, _3, _4, _5, _6, _7)                                           PP_JOIN_2(PP_JOIN_7(_0, _1, _2, _3, _4, _5, _6), _7)
#define PP_JOIN_9(_0, _1, _2, _3, _4, _5, _6, _7, _8)                                       PP_JOIN_2(PP_JOIN_8(_0, _1, _2, _3, _4, _5, _6, _7), _8)
#define PP_JOIN_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9)                                  PP_JOIN_2(PP_JOIN_9(_0, _1, _2, _3, _4, _5, _6, _7, _8), _9)
#define PP_JOIN_11(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10)                             PP_JOIN_2(PP_JOIN_10(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9), _10)
#define PP_JOIN_12(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11)                        PP_JOIN_2(PP_JOIN_11(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10), _11)
#define PP_JOIN_13(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12)                   PP_JOIN_2(PP_JOIN_12(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11), _12)
#define PP_JOIN_14(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13)              PP_JOIN_2(PP_JOIN_13(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12), _13)
#define PP_JOIN_15(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14)         PP_JOIN_2(PP_JOIN_14(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13), _14)
#define PP_JOIN_16(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15)    PP_JOIN_2(PP_JOIN_15(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14), _15)

// Chooses a value based on a condition.
#define PP_IF_0(t, f)           f
#define PP_IF_1(t, f)           t
#define PP_IF(cond, t, f)       PP_JOIN_2(PP_IF_, PP_TO_BOOL(cond))(t, f)

// Converts a condition into a boolean 0 (=false) or 1 (=true).
#define PP_TO_BOOL_0 0
#define PP_TO_BOOL_1 1
#define PP_TO_BOOL_2 1
#define PP_TO_BOOL_3 1
#define PP_TO_BOOL_4 1
#define PP_TO_BOOL_5 1
#define PP_TO_BOOL_6 1
#define PP_TO_BOOL_7 1
#define PP_TO_BOOL_8 1
#define PP_TO_BOOL_9 1
#define PP_TO_BOOL_10 1
#define PP_TO_BOOL_11 1
#define PP_TO_BOOL_12 1
#define PP_TO_BOOL_13 1
#define PP_TO_BOOL_14 1
#define PP_TO_BOOL_15 1
#define PP_TO_BOOL_16 1

#define PP_TO_BOOL(x)       PP_JOIN_2(PP_TO_BOOL_, x)

// Returns 1 if the arguments to the variadic macro are separated by a comma, 0 otherwise.
#define PP_HAS_COMMA(...)                           PP_HAS_COMMA_EVAL(PP_HAS_COMMA_ARGS(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0))
#define PP_HAS_COMMA_EVAL(...)                      __VA_ARGS__
#define PP_HAS_COMMA_ARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, ...) _16

// Returns 1 if the argument list to the variadic macro is empty, 0 otherwise.
#define PP_IS_EMPTY(...)                                                        \
    PP_HAS_COMMA                                                                \
    (                                                                           \
        PP_JOIN_5                                                           \
        (                                                                       \
            PP_IS_EMPTY_CASE_,                                              \
            PP_HAS_COMMA(__VA_ARGS__),                                      \
            PP_HAS_COMMA(PP_IS_EMPTY_BRACKET_TEST __VA_ARGS__),         \
            PP_HAS_COMMA(__VA_ARGS__ (~)),                                  \
            PP_HAS_COMMA(PP_IS_EMPTY_BRACKET_TEST __VA_ARGS__ (~))      \
        )                                                                       \
    )

#define PP_IS_EMPTY_CASE_0001           ,
#define PP_IS_EMPTY_BRACKET_TEST(...)   ,

// Retrieve the number of arguments handed to a variable-argument macro.
#define PP_VA_NUM_ARGS_HELPER(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, N, ...)    N
#define PP_VA_NUM_ARGS(...) PP_VA_NUM_ARGS_HELPER(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)

// Correctly handles the case of 0 arguments.
#define PP_NUM_ARGS(...)        PP_IF(PP_IS_EMPTY(__VA_ARGS__), 0, PP_VA_NUM_ARGS(__VA_ARGS__))

// Pass each variable in a VA_ARGS list to a macro.
#define PP_FE_0(action, X)
#define PP_FE_1(action, X) action(X)
#define PP_FE_2(action, X, ...) action(X)PP_FE_1(action, __VA_ARGS__)
#define PP_FE_3(action, X, ...) action(X)PP_FE_2(action, __VA_ARGS__)
#define PP_FE_4(action, X, ...) action(X)PP_FE_3(action, __VA_ARGS__)
#define PP_FE_5(action, X, ...) action(X)PP_FE_4(action, __VA_ARGS__)
#define PP_FE_6(action, X, ...) action(X)PP_FE_5(action, __VA_ARGS__)
#define PP_FE_7(action, X, ...) action(X)PP_FE_6(action, __VA_ARGS__)
#define PP_FE_8(action, X, ...) action(X)PP_FE_7(action, __VA_ARGS__)
#define PP_FE_9(action, X, ...) action(X)PP_FE_8(action, __VA_ARGS__)
#define PP_FE_10(action, X, ...) action(X)PP_FE_9(action, __VA_ARGS__)
#define PP_FE_11(action, X, ...) action(X)PP_FE_10(action, __VA_ARGS__)
#define PP_FE_12(action, X, ...) action(X)PP_FE_11(action, __VA_ARGS__)
#define PP_FE_13(action, X, ...) action(X)PP_FE_12(action, __VA_ARGS__)
#define PP_FE_14(action, X, ...) action(X)PP_FE_13(action, __VA_ARGS__)
#define PP_FE_15(action, X, ...) action(X)PP_FE_14(action, __VA_ARGS__)
#define PP_FE_16(action, X, ...) action(X)PP_FE_15(action, __VA_ARGS__)

#define PP_FOR_EACH(action, ...) PP_JOIN_2(PP_FE_, PP_NUM_ARGS(__VA_ARGS__))(action, __VA_ARGS__)

Definition for your macro: 宏的定义:

#define CREATE_VAR(var)     double var{smc::define_variable(data, var, #var)};
#define CREATE_VARS(...)    PP_FOR_EACH(CREATE_VAR, __VA_ARGS__)

Also, you may or may not need to use #pragma GCC system_header depending on what warning lvl you're compiling with in order to get rid of ISO C99 requires rest arguments to be used . 另外,您可能需要也可能不需要使用#pragma GCC system_header具体取决于您要编译哪种警告lvl才能摆脱ISO C99 requires rest arguments to be used You could've used #pragma GCC diagnostic ignored "-pedantic-errors" but apparently that is bugged . 您本可以使用#pragma GCC diagnostic ignored "-pedantic-errors"但显然这是有问题的 If you're using msvc you'll have to figure out the warning to disable by yourself. 如果您使用的是msvc,则必须找出警告以自行禁用。

These are called Variadic Macros, A macro can be declared to accept a variable number of arguments much as a function can. 这些称为可变参数宏,可以声明一个宏以接受可变数量的参数,就像函数可以接受的那样。

The declaration syntax is similar to that of variadic functions: an ellipsis "..." is used to indicate that one or more arguments must be passed. 声明语法类似于可变参数函数的语法:省略号“ ...”用于表示必须传递一个或多个参数。 Common compilers also permit passing zero arguments to such a macro, however. 但是,通用编译器还允许将零参数传递给此类宏。 During macro expansion each occurrence of the special identifier VA_ARGS in the macro replacement list is replaced by the passed arguments. 在宏扩展期间,宏替换列表中每次出现的特殊标识符VA_ARGS都会被传递的参数替换。

For example C99 way, also supported by VC++ compiler. 例如C99方式,VC ++编译器也支持。

#define FOO(fmt, ...) print(fmt, ##__VA_ARGS__)

void print(const char *fmt, ...) {

    va_list args;
    va_start(args, fmt);
    vsprintf(str, fmt, args);
        va_end(args);

        printf("%s\n", str);

}

This is a simple use case to show how to use vardiac maco and then how we can use the variable argument list correctly...with out looking for additional library! 这是一个简单的用例,展示了如何使用vardiac maco,然后如何正确使用变量参数列表...而无需寻找其他库!

Also have a look at 也看看

http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html

According to my understanding of your question. 根据我对您问题的理解。 you should use ellipsis(...). 您应该使用省略号(...)。 I suggest you to go through with following link. 我建议您通过以下链接进行检查。 This is help full to understand. 这是充分理解的帮助。

http://msdn.microsoft.com/en-us/library/ms177415(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms177415(v=vs.80).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM