【发布时间】:2016-03-14 10:45:02
【问题描述】:
如果我有:
#define A 1
#define B() A
#undef A
这段代码:
#if B()
std::cout << "B" << std::endl;
#else
std::cout << "not B" << std::endl;
#endif
将打印“not B”。有没有办法将 A 分配给另一个宏变量/函数,然后清除 A?
背景:在cmake's configure_file 的上下文中使用this,其中A 用#cmakedefine01 定义。
【问题讨论】:
-
宏替换在 A undef 之后进行。你不能那样做。顺便说一句,你为什么需要摆脱这个定义?
-
@LPs 我想确保人们使用
#if B()而不是#ifdef A或#if A。从这个角度来看,拥有#undef A应该更能自我记录 -
正如我所写,在 undef A 之后,B 将被其内容替换。生成的代码将始终为“非 B”。 AFAIK 你不能用定义和宏来做到这一点:“nun se po fa”;)
-
@Antonio:记录下来。您使用的是宏,而不是类 - 您不能“封装”它们。