C预处理程序,宏“重载" [英] C Preprocessor, Macro "Overloading"

查看:97
本文介绍了C预处理程序,宏“重载"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行某种宏超载",以使MACRO(某物)的扩展方式不同于MACRO(某物).

I'm trying to do some kind of Macro "Overloading", so that MACRO(something), gets expanded differently than MACRO(something, else).

使用我从此处摘录的代码段不确定它是否100%可移植)以及Boost PP库中的某些功能,我能够使其发挥作用:D

Using a snippet I got from here (I'm not sure if it's 100% portable) and some functions from the Boost PP Library, I was able to make it work :D

//THESE TWO COUNT THE NUMBER OF ARGUMENTS
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)

//THIS ONE RETURNS THE PARAMETER AT POSITION _i FROM A LIST OF __VA_ARGS__
#define VA_ARG(_i, ...) BOOST_PP_ARRAY_ELEM(_i, (VA_NARGS(__VA_ARGS__), (__VA_ARGS__)))

//AND THIS ONE IS THE 'OVERLOADED' MACRO ;)
#define TEST(...) BOOST_PP_IF(BOOST_PP_EQUAL(1, VA_NARGS(__VA_ARGS__)), function_A(VA_ARG(0, __VA_ARGS__)), \ //1 parameter
                  BOOST_PP_IF(BOOST_PP_EQUAL(2, VA_NARGS(__VA_ARGS__)), function_B(VA_ARG(0, __VA_ARGS__) + VA_ARG(1, __VA_ARGS__)), \ //2 parameters
                  BOOST_PP_IF(BOOST_PP_EQUAL(3, VA_NARGS(__VA_ARGS__)), function_C(VA_ARG(1, __VA_ARGS__) + VA_ARG(2, __VA_ARGS__)), BOOST_PP_EMPTY())) // 3 parameters and so on ...

So       TEST(a) = function_A(a)
      TEST(a, b) = function_B(a + b)
   TEST(a, b, c) = function_C(b + c)

现在,我仍然缺少我想做的另外两件事:

Now I'm still missing two other things that I want to do:

  1. (我不关心这个问题,如果我从不解决的话),我相信可以写一个MACRO:当使用"variant"的数量及其对应的"output"时,会生成类似如下的代码以上之一.像TEMPLATE(3,function_A(...),function_B(...),function_C(...))之类的东西可以生成上面的示例.

  1. (This one I don't really care if I never solve it) I believe that a MACRO can be written that when taking up the number of 'variants' and its correspondent 'output' generates a code similar like the one above. Something like TEMPLATE(3, function_A(...), function_B(...), function_C(...)) to generate the example above.

在不带参数的情况下调用TEST()会发生什么?好吧,VA_NARGS扩展为1.但是第一个参数是"(无).我试图找到一种方法来检测__VA_ARGS__中的零"参数或区分空"参数和真实参数,以扩展重载"功能以对此情况做出反应.有什么想法吗?

What happens when TEST() is called without arguments? Well, VA_NARGS expands to 1. But the first argument is ""(nothing). I'm trying to find a way to either detect 'zero' arguments in __VA_ARGS__ or to differentiate between a 'null' argument and a real one, in order to extend the 'overloading' function to react to this situation. Any ideas?

推荐答案

首先要回答您的问题2.是的,使用可变参数宏,还可以检测到空的参数列表.解释有点冗长,我已经在这里写了.将这种方法与您正在使用的boost宏结合起来应该相对容易.

To answer your question 2 first. Yes, with variadic macros it is also possible to detect an empty argument list. The explanation is a bit lengthy, I have written it up here. It should be relatively easy to combine this approach with the boost macros that you are using.

对于您的问题1,是的,这也是可能的.我认为,Boost有一些与此接近的迭代器宏,但是它们看起来有点吓人.如果我理解正确,则必须使用嵌套列表(a, (b, (c,d)))之类的东西,不太方便.

For your question 1, yes this is also possible. Boost has some iterator macros that come close to this, I think, but they look a bit scary to use. If I understand correctly you have to use something like nested lists (a, (b, (c,d))), not too convenient.

(我编写了一组宏,可以更直接地实现此目标, 但是很遗憾,该软件包尚未准备好发布.如果您对真的感兴趣,请与我私下联系.)

(I wrote a set of macros that can achieve this more directly, but unfortunately the package is not yet ready for release. Contact me in private if you are really interested in it.)

同时发布了 P99 程序包并包含大量有关宏超载"的内容,并键入通用宏.

The P99 package is published in the mean time and contains a lot of stuff over macro "overloading" and type generic macros.

这篇关于C预处理程序,宏“重载"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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