C macro to enable and disable code features

前端 未结 6 1643
Happy的楠姐
Happy的楠姐 2021-01-07 09:44

I\'ve used a code base before that had a macro system for enabling and disabling sections of code. It looked something like the following:

#define IN_USE            


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-07 10:35

    Just don't define it if it is not in use

    #if defined( WIN32 )
        #define FEATURE_A       
        #define FEATURE_B       
    #else if defined( OSX )
        #define FEATURE_C      
    #else
    
    #endif
    

    Then In you code:

    void DoFeatures()
    {
    #ifdef FEATURE_A
        // Feature A code...
    #endif
    
    #ifdef FEATURE_B
        // Feature B code...
    #endif
    
    #ifdef FEATURE_C
        // Feature C code...
    #endif
    
    #ifdef FEATURE_D // Compile error since FEATURE_D was never defined
        // Feature D code...
    #endif
    }
    

提交回复
热议问题