C ++和预处理器宏:可变参数类型 [英] C++ and preprocessor macros: variadic type

查看:52
本文介绍了C ++和预处理器宏:可变参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下面的宏为例:

#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)}; /

有没有一种方法可以通过使用一个宏 _CREATE_VAR 来简化此过程,而不是让多个实例具有不同的变量参数和相应的名称?理想情况下,无论有多少个参数,我都希望自动调用同一宏 _CREATE_VAR .

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.

推荐答案

如果您不介意调用语法略有不同,则可以使用boost.preprocessor:

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)

使用示例:

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

现在您可以使用从1到 BOOST_PP_LIMIT_SEQ 的任意数量的变量来调用它,通常为256.

Now you can call it with any number of variables from 1 to BOOST_PP_LIMIT_SEQ, which is normally 256.

一些注意事项:我使用 %% 表示该参数未使用.您可以在其中放置任何内容(将其传递给内部宏的 maData 参数,我们不使用它).

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.

这篇关于C ++和预处理器宏:可变参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆