How to generate a newline in a cpp macro?

后端 未结 7 1154
北恋
北恋 2020-12-03 09:29

How do I write a cpp macro which expands to include newlines?

7条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 10:02

    Not quite sure what you're asking here. Do you want a macro on multiple lines?

    #define NEWLINE_MACRO(x) line1 \
    line2 \
    line3
    

    Additionally, if you would like to include a literal in your macro:

    #define NEWLINE_MACRO(x) ##x
    

    what you you put in x will be put in place of ##x, so:

    NEWLINE_MACRO( line1 ) // is replaced with line1
    

    This can be helpful for making custom global functions then just need part of the function name changed.

    Also:

    #define NEWLINE_MACRO(x) #x // stringify x
    

    Will put quotes around x

提交回复
热议问题