先看一下C++11标准里的字义(2.14.5):
raw-string:
" d-char-sequenceopt ( r-char-sequenceopt ) d-char-sequenceopt "
r-char-sequence:
r-char
r-char-sequence r-char
r-char:
any member of the source character set, except
a right parenthesis ) followed by the initial d-char-sequence
(which may be empty) followed by a double quote ".
d-char-sequence:
d-char
d-char-sequence d-char
d-char:
any member of the basic source character set except:
space, the left parenthesis (, the right parenthesis ), the backslash \,
and the control characters representing horizontal tab,
vertical tab, form feed, and newline.
简单的翻译一下:
raw-string:
"D(R)D"
R:
r | Rr
r:
除了 )D" 以外的任何字符串
D:
d | Dd
d:
除了空格,左括号,右括号,转义符以及控制符的任意字符串
下面来简单地分析一下:之所以这样定义语法,就是为了避免字符串的内部与结束符混淆。正常情况下字符串是位于R" "之间的,结束符就是"。可是字符串内部可以包含"怎么办呢?比如R"fewga"rgare",改结束符,变成)",这样就变成R"(fewga"rgare)",不会混淆了。可是问题又来了,如果字符串内包含)"呢?于是又引入了d-char-sequenceopt(可以为空),估且叫它分隔串吧,变成R"--(fewga)"rgare)--"这样的形式,于是问题解决。由于分隔串是用户指定的,所以可以使用不固定的结束符,不会混淆(除非有人太二)。字符串内有)-",我就用)#",有)#",我就用)**"。