我可以追加到preprocessor宏? [英] Can I append to a preprocessor macro?

查看:183
本文介绍了我可以追加到preprocessor宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在标准C或GNU扩展到附加的东西宏定义?给宏的例如的,定义为结果
的#define清单富栏结果
我可以追加 BAS ,以便它列表的#define清单富酒吧浅

Is there any way in standard C—or with GNU extensions—to append stuff to a macro definition? E.g., given a macro defined as
#define List foo bar
can I append bas so that it List expands as if I’d defined it
#define List foo bar bas?

我希望我可以做这样的事情:

I was hoping I could do something like this:

#define List    foo bar bas

#define List_   Expand(List)
#undef List
#define List    Expand(List_) quux

但我不能想出如何定义展开()宏,以便它会做我想做的。

but I can’t figure out how to define the Expand() macro so it’ll do what I want.

动机:
我与歧视/标签联合演奏沿着这些路线:

Motivation: I’m playing with discriminated/tagged unions along these lines:

struct quux_foo { int x; };
struct quux_bar { char *s; };
struct quux_bas { void *p; };

enum quux_type {quux_foo, quux_bar, quux_bas};

struct quux {
    enum quux_type type;
    union {
        struct quux_foo foo;
        struct quux_bar bar;
        struct quux_bas bas;
    } t;
};

我想这是X-宏的好地方。如果我定义一个宏结果
的#define quux_table X(富)X(栏)X(BAS)结果
枚举和放大器;结构因此可以定义,永远走不出同步:

I figure this is a good place for the X-macro. If I define a macro
#define quux_table X(foo) X(bar) X(bas)
the enumeration & structure can be defined thus, and never get out of sync:

#define X(t) quux_ ## t,
enum quux_type {quux_table};
#undef X

#define X(t) struct quux_ ## t t;
struct quux {
    enum quux_type type;
    union {quux_table} t;
};
#undef X

当然, QUUX _ * 结构可以不同步,所以我愿意做这样的事情,只有合法:

Of course, the quux_* structures can get out of sync, so I’d like to do something like this, only legally:

struct quux_foo { int x; };
#define quux_table quux_table X(foo)

struct quux_bar { char *s; };
#define quux_table quux_table X(bar)

struct quux_bas { void *p; };
#define quux_table quux_table X(bas)

(好吧,我的真正的希望能够做的是一样的东西结果
member_struct(QUUX,富){INT X; }; 结果
但我清楚地知道,宏不能(再)宏内定义的。)

(Well, what I really want to be able to do is something like
member_struct(quux, foo) { int x; };
but I’m well aware that macros cannot be (re)defined from within macros.)

总之,这是我的激励的例子。有没有办法做到这一点?

Anyhow, that’s my motivating example. Is there a way to accomplish this?

升压。preprocessor例子都很好,如果你能告诉我如何与该库的X宏技术的工作。

Boost.Preprocessor examples are fine, if you can show me how to make the X-macro technique work with that library.

推荐答案

实际上,没有。

宏懒洋洋地评估。当你的#define List_展开(列表),其替换列表是四个令牌序列展开列表。没有任何方式来扩大宏成替换列表。

Macros are lazily evaluated. When you #define List_ Expand(List), its replacement list is the sequence of four tokens Expand, (, List, and ). There isn't any way to expand a macro into a replacement list.

所有宏替换时需要宏被调用的地方。

All macro replacement takes place when a macro is invoked.

我建议考虑使用升压。preprocessor库自动code一代。这是一些工作,但是你可以完成一些<一个href=\"http://stackoverflow.com/questions/3818277/can-i-get-polymorphic-behavior-without-using-virtual-functions/3825181#3825181\">fairly IM pressive东西的使用它。它应该是用C完全兼容。

I'd recommend looking at using the Boost.Preprocessor library for automatic code generation. It's a bit of work, but you can accomplish some fairly impressive things using it. It should be fully compatible with C.

这篇关于我可以追加到preprocessor宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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