There is a very convenient feature introduced in C++11 called raw string literals, which are strings with no escape characters. And instead of writing this:
regex mask("\\t[0-9]+\\.[0-9]+\\t\\\\SUB");
You can simply write this:
regex mask(R"(\t[0-9]+\.[0-9]+\t\\SUB)");
Quite more readable. However, note extra parenthesis around the string one have to place to define a raw string literal.
My question is, why do we even need these? For me it looks quite ugly and illogical. Here are the cons what I see:
- Extra verbosity, while the whole feature is used to make literals more compact
- Hard to distinguish between the body of the literal and the defining symbols
That's what I mean by the hard distinguishing:
"good old usual string literal"
^- body inside quotes -^
R"(new strange raw string literal)"
^- body inside parenthesis -^
And here is the pro:
- More flexibility, more characters available in raw strings, especially when used with the delimiter:
"delim( can use "()" here )delim"
But hey, if you need more flexibility, you have old good escapeable string literals. Why the standard committee decided to pollute the content of every raw string literal with these absolutely unnecessary parenthesis? What was the rationale behind that? What are the pros I didn't mention?
UPD The answer by Kerrek is great, but it is not an answer, unfortunately. Since I already described that I understand how it works and what benefits does it give. Five years passed since I've asked this question, and still there is no answer. And I am still frustrated by this decision. One could say that this is a matter of taste, but I would disagree. How many spaces do you use, how do you name your variables, is this SomeFunction()
or some_function()
- this is the matter of taste. And I can really easily switch from one style to another.
But this?.. Still feels awkward and clumsy after so many years. No, this is not about the taste. This is about how we want to cover all possible cases no matter what. We doomed to write these ugly parens every time we need to write a Windows-specific path, or a regular expression, or a multi-line string literal. And for what?.. For those rare cases when we actually need to put "
in a string? I wish I was on that committee meeting where they decided to do it this way. And I would be strongly against this really bad decision. I wish. Now we are doomed.
Thank you for reading this far. Now I feel a little better.
UPD2 Here are my alternative proposals, which I think both would be MUCH better than existing.
Proposal 1. Inspired by python. Cannot support string literals with triple quotes: R"""Here is a string literal with any content, except for triple quotes, which you don't actually use that often."""
Proposal 2. Inspired by common sense. Supports all possible string literals, just like the current one: R"delim"content of string"delim"
. With empty delimiter: R""Looks better, doesn't it?""
. Empty raw string: R""""
. Raw string with double quotes: R"#"Here are double quotes: "", thanks"#"
.
Any problems with these proposals?
R";-](R"(this is a basic raw string literal as text inside a more complex one)");-]"
– NorthamptonR"(...))"
's ugliness serves as a warning sign for me that "this is a string literal, be careful", since it stands out so much. – Conciseness"
in a raw string are "rare" is probably part of the problem. It's not that there is "no answer". There is an answer; you just don't agree with it. If your definition of what constitutes an "answer" is "something that convinces me to change my mind on this", then your question is too opinionated. The justification has been provided; your agreement with it is not required. – Pregnant