Home > Net >  Adding comment in raw string literal
Adding comment in raw string literal

Time:11-03

I have a raw string literal:

const char* s1 = R"foo(
Hello
  World
)foo";

I would like to add a comment inside this string literal, e.g.:

const char* s1 = R"foo(
Hello
// Say hello to the whole world because we don't know who will run the program.
  World
)foo";

This comment is actually used as part of the string. Is there any way to escape this comment to be parsed as an actual comment (or does that defeat the purpose of a raw string)?

I can obviously do ugly things like split the string in two add a comment between the two parts and concatenate them but I am looking for something more elegant.

CodePudding user response:

I think the closest thing is to "escape it" with the raw string end and start delimiters:

const char* s1 = R"foo(
Hello)foo"
// Say hello to the whole world because we don't know who will run the program.
R"foo(
  World
)foo";
  •  Tags:  
  • c
  • Related