【问题标题】:PreProcessing #define预处理#define
【发布时间】:2011-02-16 15:40:06
【问题描述】:

我无法理解预处理器的工作原理以及## 在这个特定示例中代表什么

#include <stdio.h>

#define TEMP_KEY(type,Key) (TEMP_##type | Key)

enum TEMPKey_Type
{
    TEMP_UNKNOWN = 0,
    TEMP_SPECIAL ,
    TEMP_UNICODE
};

enum Actual_Key
{
    TEMP_RIGHT = TEMP_KEY(UNKNOWN,0x1),
    TEMP_LEFT = TEMP_KEY(SPECIAL,0x1),
    TEMP_UP = TEMP_KEY(UNICODE,0x1)
};

int main()
{
    printf("\n Value of TEMP_RIGHT : %d ",TEMP_RIGHT);
    printf("\n Value of TEMP_LEFT : %d ",TEMP_LEFT);
    printf("\n Value of TEMP_UP : %d ",TEMP_UP);

    return 0;
}

这是怎么做到的 #define TEMP_KEY(type,Key) (TEMP_##type | Key) 工作或在预处理过程中TEMP_##type 究竟是如何替换的?

【问题讨论】:

    标签: c c-preprocessor string-concatenation stringification


    【解决方案1】:

    “##”表示连接。因此TEMP_RIGHT = TEMP_KEY(UNKNOWN,0x1) 变为TEMP_RIGHT = TEMP_UNKNOWN | 0x1,(“TEMP_”和“UNKNOWN”连接在一起)

    【讨论】:

    • String 连接,即 this.也称为“胶水”。
    【解决方案2】:

    ## 是#define 指令中的连接运算符。

    例如,TEMP_KEY(UNICODE,0x1) 调用的 TEMP_##type 生成下一个代码:

    (TEMP_UNICODE | 0x1)
    

    【讨论】:

    • 你知道了,你能给我提供任何链接以了解更多信息
    • @Totie - 你可以阅读this。如需更复杂的信息,可以推荐有关 c++ 元编程的书籍 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多