【问题标题】:Preprocessor Directive: #elif not defined?预处理器指令:#elif 未定义?
【发布时间】:2013-02-11 08:28:23
【问题描述】:

是否有一个预处理指令来检查一个常量是否没有定义。我知道#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 未定义?
  • 你想做什么?您不能定义包含其他预处理器指令的宏。您不能将 #define#if#elif 作为宏的一部分。您的宏必须重新设计以确保它没有内部“分支”。所有宏分支都必须“在外部”完成。它不能“嵌入”到宏中。

标签: c-preprocessor preprocessor-directive


【解决方案1】:

怎么样

#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)
    //.....

【讨论】:

    猜你喜欢
    • 2012-02-19
    • 2020-04-07
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多