带有宏值串联的C ++`ifdef` [英] C++ `ifdef` with concatenation of macros values

查看:95
本文介绍了带有宏值串联的C ++`ifdef`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否可以实现类似于以下代码的内容:

Can I achieve something similar to following code:

#define MODULE base
#if defined (MODULE ## _dll)      <-- this should do `#ifdef base_dll`
    ...
#else
    ...
#endif

第二行显然是错误的。我可以以某种方式执行此操作吗?

second line is obviously wrong. Can I do this somehow?

谢谢

推荐答案

我不这样做认为可以像这样检查令牌粘贴的宏的定义(至少我不知道这种方式),但是您可以这样做:

I don't think it is possible to check the definition of token-pasted macro like that (at least I don't know the way) but you can do this:

#define JOIN_INTERNAL(a,b) a ## b
#define JOIN(a,b) JOIN_INTERNAL(a,b)

// switch 1/0
#define base_dll 1

#define MODULE base

#if JOIN(MODULE,_dll)
// the base_dll is 1
#else
// the base_dll is 0 or not defined (in MSVC at least)
#endif

也许,如果您描述了您实际想要实现的目标,那么可能还有另一种方法。

Perhaps if you describe what do you actually want to achieve there might be another way to do that.

这篇关于带有宏值串联的C ++`ifdef`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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