超载C / C ++ $ P在它的参数结构$ pprocessor宏 [英] Overload C/C++ preprocessor macro on structure of its argument

查看:113
本文介绍了超载C / C ++ $ P在它的参数结构$ pprocessor宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个preprocessor宏,确实,如果它的说法有一点是令牌的括号中的元组,像这样的:

I would like to write a preprocessor macro that does one thing if it's argument is a parenthesized tuple of tokens, like this:

MY_MACRO((x, y))

和别的东西,如果它只是一个单一的道理,就像这样:

and something else if it's just a single token, like this:

MY_MACRO(x)

这可能吗?

如何 MY_MACRO(X) MY_MACRO(XY)

请注意,我不是根据参数的个数重载 - 这在所有的情况下,一元的宏

Note that I am not trying to overload based on the number of arguments - it's a unary macro in all cases.

修改:我很愿意,如果他们帮助使用复杂的宏

EDIT: I am willing to use variadic macros if they help

推荐答案

对于第一个问题,下面的宏可能会满足你的目的:

As for your first question, the following macros might meet your purpose:

#define CONCAT_( x, y ) x ## y
#define CONCAT( x, y ) CONCAT_( x, y )
#define IS_SINGLE_1(...) 0
#define IGNORE(...)
#define IS_SINGLE_2_0           0 IGNORE(
#define IS_SINGLE_2_IS_SINGLE_1 1 IGNORE(
#define IS_SINGLE( x ) CONCAT( IS_SINGLE_2_, IS_SINGLE_1 x ) )
IS_SINGLE((x, y)) // 0
IS_SINGLE(x)      // 1

IS_SINGLE 被扩展为1,如果该参数是一个道理,
否则,0。

Macro IS_SINGLE is expanded to 1 if the argument is single token, otherwise, 0.

希望这有助于

这篇关于超载C / C ++ $ P在它的参数结构$ pprocessor宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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