预处理指令:#elif未定义?

5

有没有预处理指令可以检查一个常量是否未定义。我知道#ifndef指令,但我也在寻找一个#elif not defined指令。是否存在#elif not defined指令?

这是我如何使用它:

#define REGISTER_CUSTOM_CALLBACK_FUNCTION(callbackFunctName) \
    #ifndef CUSTOM_CALLBACK_1 \
        #define CUSTOM_CALLBACK_1 \
        FORWARD_DECLARE_CALLBACK_FUNCTION(callbackFunctName) \
    #elif not defined CUSTOM_CALLBACK_2 \
        #define CUSTOM_CALLBACK_2  \
        FORWARD_DECLARE_CALLBACK_FUNCTION(callbackFunctName) \
    #elif not not defined CUSTOM_CALLBACK_3 \
        #define CUSTOM_CALLBACK_3  \
        FORWARD_DECLARE_CALLBACK_FUNCTION(callbackFunctName) \
    #endif

#elif not not defined CUSTOM_CALLBACK_3 不是未定义吗? - JustMaximumPower
你想做什么?你不能定义包含其他预处理指令的宏。你不能使 #define#if#elif 成为宏的一部分。你的宏必须重新设计,以确保它没有内部“分支”。所有宏分支都必须在“外部”完成。它不能被“嵌入”到一个宏中。 - AnT stands with Russia
1个回答

16

如何处理

#elif !defined(...)

但是你还有更大的问题——末尾的\会排除其他指令,或者说使它们非法。因此,即使有有效的语法,你的定义也无法实现你想要的功能。

你需要将初始定义移到条件内部。

#ifndef CUSTOM_CALLBACK_1
    #define CUSTOM_CALLBACK_1 
    #define REGISTER_CUSTOM_CALLBACK_FUNCTION(callbackFunctName) \
    FORWARD_DECLARE_CALLBACK_FUNCTION(callbackFunctName) 
#elif !defined(CUSTOM_CALLBACK_2)
    //.....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接