How to escape backslash in // comment
Asked Answered
C

5

5

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?

Countryfied answered 17/5, 2015 at 11:12 Comment(3)
Is there any particular reason that the comment must end with a backslash?Theta
I ran into this when I tried to use the :\ face in a comment :\Phifer
Old post, but for me just adding a (white)space after the backslash seemed to work...Unlive
S
6

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
Smallman answered 17/5, 2015 at 17:53 Comment(0)
G
5

While this is not a technical solution, I would take that as a hint to write a better comment:

// use \ (backslash) instead of the normal slash here, because ...
Gourd answered 17/5, 2015 at 12:42 Comment(0)
G
1

You can use Grave Accent (`)

#include<iostream>

using namespace std;

int main()
{

    // print `\`
    cout << "DONE" << endl;

    return 0;
}
Gemmation answered 17/5, 2015 at 12:38 Comment(1)
A regular quote or double quote should work. Anything after the backslash, except whitespace and newline, should work.Smallman
G
0

Maybe you have code like this:

#define foo {int x = 123; \
cout << x << endl; \
}

And you want to comment it out:

//#define foo {int x = 123; \
//cout << x << endl; \
//}

Above code should work fine. On the other hand, if you write:

#define foo {int x = 123; \
cout << x << endl; \
} \

Then foo may work (if next line is empty) but you get in trouble if you comment it out. Just remove the last back slash. Otherwise don't end the line with backslash unless it's meant to connect...

Greenleaf answered 17/5, 2015 at 14:59 Comment(0)
G
0

In my case the other solutions (that put something after the backslash) don't work because I use the backslash as ASCII-art:

menuItem == output1Slider ||  // \
menuItem == output2Slider ||  //  > No need to skip these checks when not available
menuItem == output3Toggle ) { // /

My chosen solution is to use Unicode-art:

menuItem == output1Slider ||  // ⎫
menuItem == output2Slider ||  // ⎬ No need to skip these checks when not available
menuItem == output3Toggle ) { // ⎭
Glabrate answered 19/6, 2024 at 10:25 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.