【问题标题】:Using/Copying the value of a macro variable in/to a macro function before undefining it: is it possible?在取消定义之前在宏函数中使用/复制宏变量的值:可能吗?
【发布时间】: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:记录下来。您使用的是宏,而不是类 - 您不能“封装”它们。

标签: c++ c cmake macros


【解决方案1】:

没有。

宏不是类,没有那种复杂程度。

如果一个宏是根据另一个宏定义的,则两个宏都必须保持存在才能使替换正常工作。

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多