How to make G++ preprocessor output a newline in a macro?

前端 未结 8 973
隐瞒了意图╮
隐瞒了意图╮ 2020-12-01 15:55

Is there a way in gcc/g++ 4.* to write a macro that expands into several lines?

The following code:

#define A X \\ Y

Expands into

8条回答
  •  不知归路
    2020-12-01 16:27

    Putting a side the fact that not being able to put newlines in macros create unreadable code, making it harder to debug preprocessor outputs. It is true that C and C++ might not care about newlines, but the C preprocessor does.

    I would really like to make a macro ConditionalDefine(x,y) that outputs the following.

        #ifdef x
        #define y
        #endif
    

    The following defines do something close:

        #define hash #
        #define nl
        #define _def_p8(A,B) A ifdef _P8_K60_BOARD_ A define B  A endif
        #define X_def_p8(A,B) _def_p8(A,B)
        #define def_p8(A) X_def_p8(nl hash,A)
    

    expanding the following:

        def_p8(PTPD_DBGA 0)
    

    results in:

        # ifdef _P8_K60_BOARD_ # define PTPD_DBGA 0    # endif
    

    But without being able to put new lines in before the hashes it would not work as intended. It is also annoying the hoops you have to jump through just to get that close.

提交回复
热议问题