c++escapingcommentsc-preprocessorclang++

How to escape backslash in // comment


I have a comment that ends with backslash. Something like

...

// use \

..

Clang(++) warned me, that this is multi-line comment

warning: multi-line // comment [-Wcomment]
    // use \
           ^

So I try add some whitespace at the end, but didn't help. Can I escape backslash somehow?


Solution

  • The foundation of the issue is the definition of a line continuation.

    When a line ends with a backslash-newline combination or <backslash><whitespace><newline> combination, the compiler appends the next line of text to the present line of text. This can be demonstrated with macros:

    #define ME {\
    cout << "me\n" \
    }
    

    The above will be treated as the single line:

    #define ME {cout << "me\n"}
    

    The compiler is complaining because your "//" comment extends to the next line because the '\' continuation character.

    Solution:
    Put other characters after the '\'.
    Examples:

      '\'
      \ ending character