如何使预处理器宏贪婪? [英] How to make a preprocessor macro greedy?

查看:112
本文介绍了如何使预处理器宏贪婪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下预处理程序宏。它用于帮助Doxygen文档,因为Doxygen在C ++和某些模板typedef方面存在问题:

We have the following preprocessor macro. Its used to help with Doxygen documentation because Doxygen has troubles with C++ and some template typedefs:

#if defined(DOXYGEN_PROCESSING)
# define DOCUMENTED_TYPEDEF(x, y) class y : public x {};
#else
# define DOCUMENTED_TYPEDEF(x, y) typedef x y;
#endif

X 是非模板,或只有一个模板参数。但是,如果 X 是具有多个参数的模板:

It works great when X is a non-template or has only one template parameter. However, if X is a template with multiple parameters:

DOCUMENTED_TYPEDEF(Foo<R,S>,Bar);

然后由于字符串被拆分为 Foo< R而导致编译错误 S>,Bar (并且不会生成文档)。

Then it results in compile errors because the string is split into Foo<R and S>,Bar (and it does not generate the documentation).

如何使预处理器宏贪婪?

How do I make a preprocessor macro greedy?

推荐答案

无法更改预处理器将参数解析为宏的方式。不在括号内的逗号始终会分隔宏参数。

There is no way to change how the preprocessor parses the arguments to a macro. Commas that are not within parentheses always separate macro arguments.

可能可以执行的操作是

DOCUMENTED_TYPEDEF((Foo<R,S>), Bar);

当然,这只有在内部括号可以出现在宏扩展中的情况下才有效。我不记得我是否会在您显示的上下文中引起麻烦。

but of course this only works if it's okay for the inner parentheses to appear in the expansion of the macro. I don't remember off the top of my head if this will cause problems in the contexts you are showing.

如果可以使用C99可变参数宏,则可以使用它们可以消除多余的括号:

If it's OK to require C99 variadic macros, you can use them to get rid of the extra parentheses:

#define STRIP_PARENS(...) __VA_ARGS__
#if defined(DOXYGEN_PROCESSING)
# define DOCUMENTED_TYPEDEF(x, y) class y : public STRIP_PARENS x {};
#else
# define DOCUMENTED_TYPEDEF(x, y) typedef STRIP_PARENS x y;
#endif

DOCUMENTED_TYPEDEF((Foo<R,S>), Bar);

但是现在您总是必须在第一个参数是DOCUMENTED_TYPEDEF。

but now you always have to put an extra pair of parentheses around the first argument to DOCUMENTED_TYPEDEF.

这篇关于如何使预处理器宏贪婪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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