How to generate a newline in a cpp macro?

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

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

7条回答
  •  暖寄归人
    2020-12-03 10:19

    Use the \ at the end of the line. I've seen a lot of C macos where they use a do...while(0)

    #define foo() do \
    {
      //code goes here \
      \
      \
    }while(0);
    

    Also, remember to use parenthases in many instances.

    Example:

    #define foo(x) a+b
    //should be
    #define foo(x) (a+b)
    

提交回复
热议问题