C++ and preprocessor macros: variadic type

♀尐吖头ヾ 提交于 2020-01-04 08:20:03

问题


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? Ideally, I would like to automatically call the same macro _CREATE_VAR regardless of how many arguments.


回答1:


If you don't mind a slightly different call syntax, you can use boost.preprocessor for that:

#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.

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).

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.




回答2:


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.

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. You could've used #pragma GCC diagnostic ignored "-pedantic-errors" but apparently that is bugged. If you're using msvc you'll have to figure out the warning to disable by yourself.




回答3:


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




回答4:


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.

For example C99 way, also supported by VC++ compiler.

#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!

Also have a look at

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



来源:https://stackoverflow.com/questions/15609533/c-and-preprocessor-macros-variadic-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!