2013-02-11 162 views
3

是否有预处理程序指令检查是否未定义常量。我知道#ifndef指令,但我也在寻找#elif not defined指令。 #elif not defined是否存在?预处理器指令:#elif未定义?

这是我会怎么使用它:

#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 
+0

为什么不使用ifndef? – Dariusz 2013-02-11 08:30:14

+0

'#elif未定义CUSTOM_CALLBACK_3'未定义? – JustMaximumPower 2013-02-11 08:30:26

+0

你想做什么?您不能定义包含其他预处理器指令的宏。你不能使'#define'或'#if'或'#elif'成为宏的一部分。您的宏必须重新设计,以确保它没有内部“分支”。所有宏观分支都必须在“外部”完成。它不能被“嵌入”到宏中。 – AnT 2013-02-11 08:33:47

回答

12

怎么样

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