Skip to main content
seems it was not the OP having this concern :)
Source Link
Yılmaz Durmaz
  • 3.2k
  • 19
  • 36

I will make an addition about OP'sa concern in one of the comments:

But here in the code the R is defined as "x" and after expansion of the #define the code is const char* s = "x""y"; and there isn't any R"(.

The code fragment in the question is to show invalid uses of the Raw Strings. Let me get the actual 3-lines of code here:

#define R "x" const char* s = R"y"; // ill-formed raw string literal, not "x" "y" const char* s2 = R"(a)" "b)"; // a raw string literal followed by a normal string literal 
  • The first line is there to not get confused by a macro. macros are preprocessed code fragments that replace parts in the source. Raw String, on the other hand, is a feature of the language that is "parsed" according to language rules.
  • The second line is to show the wrong use of it. Correct way would be R"(x)" where you need parenthesis in it.
  • And the last is to show how it can be a pain if not written carefully. The string inside parenthesis CANNOT include closing sequence of raw string. A correction might be R"_(a)" "b)_". _ can be replaced by any character (but not parentheses, backslash and spaces) and any number of them as long as closing sequence is not included inside: R"___(a)" "b)___" or R"anything(a)" "b)anything"

So if we wrap these correction within a simple C++ code:

#include <iostream> using namespace std; #define R "x" // This is just a macro, not Raw String nor definition of it const char* s = R"(y)"; // R is part of language, not a macro const char* s2 = R"_(a)" "b)_"; // Raw String shall not include closing sequence of characters; )_" int main(){ cout << s <<endl << s2 <<endl << R <<endl; } 

then the output will be

y a)" "b x 

I will make an addition about OP's concern in one of comments:

But here in the code the R is defined as "x" and after expansion of the #define the code is const char* s = "x""y"; and there isn't any R"(.

The code fragment in the question is to show invalid uses of the Raw Strings. Let me get the actual 3-lines of code here:

#define R "x" const char* s = R"y"; // ill-formed raw string literal, not "x" "y" const char* s2 = R"(a)" "b)"; // a raw string literal followed by a normal string literal 
  • The first line is there to not get confused by a macro. macros are preprocessed code fragments that replace parts in the source. Raw String, on the other hand, is a feature of the language that is "parsed" according to language rules.
  • The second line is to show the wrong use of it. Correct way would be R"(x)" where you need parenthesis in it.
  • And the last is to show how it can be a pain if not written carefully. The string inside parenthesis CANNOT include closing sequence of raw string. A correction might be R"_(a)" "b)_". _ can be replaced by any character (but not parentheses, backslash and spaces) and any number of them as long as closing sequence is not included inside: R"___(a)" "b)___" or R"anything(a)" "b)anything"

So if we wrap these correction within a simple C++ code:

#include <iostream> using namespace std; #define R "x" // This is just a macro, not Raw String nor definition of it const char* s = R"(y)"; // R is part of language, not a macro const char* s2 = R"_(a)" "b)_"; // Raw String shall not include closing sequence of characters; )_" int main(){ cout << s <<endl << s2 <<endl << R <<endl; } 

then the output will be

y a)" "b x 

I will make an addition about a concern in one of the comments:

But here in the code the R is defined as "x" and after expansion of the #define the code is const char* s = "x""y"; and there isn't any R"(.

The code fragment in the question is to show invalid uses of the Raw Strings. Let me get the actual 3-lines of code here:

#define R "x" const char* s = R"y"; // ill-formed raw string literal, not "x" "y" const char* s2 = R"(a)" "b)"; // a raw string literal followed by a normal string literal 
  • The first line is there to not get confused by a macro. macros are preprocessed code fragments that replace parts in the source. Raw String, on the other hand, is a feature of the language that is "parsed" according to language rules.
  • The second line is to show the wrong use of it. Correct way would be R"(x)" where you need parenthesis in it.
  • And the last is to show how it can be a pain if not written carefully. The string inside parenthesis CANNOT include closing sequence of raw string. A correction might be R"_(a)" "b)_". _ can be replaced by any character (but not parentheses, backslash and spaces) and any number of them as long as closing sequence is not included inside: R"___(a)" "b)___" or R"anything(a)" "b)anything"

So if we wrap these correction within a simple C++ code:

#include <iostream> using namespace std; #define R "x" // This is just a macro, not Raw String nor definition of it const char* s = R"(y)"; // R is part of language, not a macro const char* s2 = R"_(a)" "b)_"; // Raw String shall not include closing sequence of characters; )_" int main(){ cout << s <<endl << s2 <<endl << R <<endl; } 

then the output will be

y a)" "b x 
Source Link
Yılmaz Durmaz
  • 3.2k
  • 19
  • 36

I will make an addition about OP's concern in one of comments:

But here in the code the R is defined as "x" and after expansion of the #define the code is const char* s = "x""y"; and there isn't any R"(.

The code fragment in the question is to show invalid uses of the Raw Strings. Let me get the actual 3-lines of code here:

#define R "x" const char* s = R"y"; // ill-formed raw string literal, not "x" "y" const char* s2 = R"(a)" "b)"; // a raw string literal followed by a normal string literal 
  • The first line is there to not get confused by a macro. macros are preprocessed code fragments that replace parts in the source. Raw String, on the other hand, is a feature of the language that is "parsed" according to language rules.
  • The second line is to show the wrong use of it. Correct way would be R"(x)" where you need parenthesis in it.
  • And the last is to show how it can be a pain if not written carefully. The string inside parenthesis CANNOT include closing sequence of raw string. A correction might be R"_(a)" "b)_". _ can be replaced by any character (but not parentheses, backslash and spaces) and any number of them as long as closing sequence is not included inside: R"___(a)" "b)___" or R"anything(a)" "b)anything"

So if we wrap these correction within a simple C++ code:

#include <iostream> using namespace std; #define R "x" // This is just a macro, not Raw String nor definition of it const char* s = R"(y)"; // R is part of language, not a macro const char* s2 = R"_(a)" "b)_"; // Raw String shall not include closing sequence of characters; )_" int main(){ cout << s <<endl << s2 <<endl << R <<endl; } 

then the output will be

y a)" "b x