在C ++ 11的原始字符串文字R“(...)”中括号的原理是什么? [英] What is the rationale for parenthesis in C++11's raw string literals R"(...)"?

查看:287
本文介绍了在C ++ 11的原始字符串文字R“(...)”中括号的原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中引入了一个非常方便的功能,称为原始字符串字面值,它是没有转义字符的字符串。而不是这样写:

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");

您可以这样写:

  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:


  • 额外的冗长,而整个功能用于使文字更紧凑

  • 很难区分文本的主体和定义的符号

这是我的意思是硬的区别:

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  -^

这里是pro:


  • 更灵活,原始字符串中有更多字符,特别是与分隔符一起使用时:delim(can use()here)delim

  • 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?

推荐答案

括号的目的是允许您指定自定义分隔符:

The purpose of the parentheses is to allow you to specify a custom delimiter:

R"foo(Hello World)foo"   // the string "Hello World"

在您的示例中,在典型使用中,分隔符仅为空,因此原始字符串由序列 R(

In your example, and in typical use, the delimiter is simply empty, so the raw string is enclosed by the sequences R"( and )".

允许任意分隔符是一个设计决策反映了希望提供一个完整的解决方案,没有奇怪的限制或边缘情况。您可以选择字符串中不出现的任何序列作为分隔符。

Allowing for arbitrary delimiters is a design decision that reflects the desire to provide a complete solution without weird limitations or edge cases. You can pick any sequence of characters that does not occur in your string as the delimiter.

没有这个,您会遇到麻烦,如果字符串本身包含(如果你只是想要 R...作为原始字符串语法)或(如果分隔符为空)。这两个是完全常见的和频繁的字符序列,特别是在正则表达式中,所以如果决定是否使用原始字符串取决于字符串的具体内容,这将是令人难以置信的恼人。

Without this, you would be in trouble if the string itself contained something like " (if you had just wanted R"..." as your raw string syntax) or )" (if the delimiter is empty). Both of those are perfectly common and frequent character sequences, especially in regular expressions, so it would be incredibly annoying if the decision whether or not you use a raw string depended on the specific content of your string.

请记住,在原始字符串中没有其他的转义机制,所以最好的方法是连接字符串字面量,这是非常不切实际的。通过允许自定义分隔符,您所需要做的只是选择一个不寻常的字符序列一次,并且可以在非常少的情况下修改它。

Remember that inside the raw string there's no other escape mechanism, so the best you could do otherwise was to concatenate pieces of string literal, which would be very impractical. By allowing a custom delimiter, all you need to do is pick an unusual character sequence once, and maybe modify it in very rare cases when you make a future edit.

但是再次强调,即使空分隔符已经有用,因为 R(...)语法允许你放置裸引号标记在您的字符串。这本身就是一个收获。

But to stress once again, even the empty delimiter is already useful, since the R"(...)" syntax allows you to place naked quotation marks in your string. That by itself is quite a gain.

这篇关于在C ++ 11的原始字符串文字R“(...)”中括号的原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆