Multiple preprocessor directives on one line in C++

前端 未结 3 1836
盖世英雄少女心
盖世英雄少女心 2021-02-07 16:26

A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line?

Such a line would look like this:

         


        
3条回答
  •  独厮守ぢ
    2021-02-07 16:48

    A few years late - but here is a pattern I use every day:

    #ifdef _DEBUG
        #define DEBUGTRACE(debuglevel, code)    \
            if ( traceLevelActive(debuglevel) ) \
                {code}
    #else
        #define DEBUGTRACE(debuglevel, code)
    #endif
    

    Usage:

    DEBUGTRACE(DEBUG_LEVEL2, printf("you are in debug mode"); );
    

    or, more elaborate:

    DEBUGTRACE(DEBUG_LEVEL2, {
        if (somethingistrue)
            printf("Debugging is TRUE");
        else
            foo();
    });
    

    If _DEBUG is defined, then your code is include. If not, then nothing is not compiled.

提交回复
热议问题