C++ `ifdef` with concatenation of macros values

淺唱寂寞╮ 提交于 2019-12-07 04:17:59

问题


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?

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/34109448/c-ifdef-with-concatenation-of-macros-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!