为什么在尾随反斜杠之前解析原始字符串文字?
Why is raw string literal parsed before trailing backslash?
从Phases of translation开始,反斜杠连接下一行发生在阶段2,字符串文字评估发生在阶段3。那么为什么下面的代码之前进行字符串评估?
#include<string>
#include<iostream>
int main() {
std::string s = R"(before\
after)";
std::cout << s;
}
给出:
before\
after
而不是:
beforeafter
Phase 2
- Whenever backslash appears at the end of a line (immediately followed by zero or more whitespace characters other than new-line followed by (since C++23) the newline character), these characters are deleted, combining two physical source lines into one logical source line. [...]
Phase 3
- The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following:
a) header names such as or "myfile.h"
b) identifiers
c) preprocessing numbers
d) character and string literals , including user-defined (since C++11) [...]
原始字符串文字 explicitly undo phases 1&2:
If the next character begins a sequence of characters that could be the prefix and initial double quote of a raw string literal, such as R", the next preprocessing token shall be a raw string literal. Between the initial and final double quote characters of the raw string, any transformations performed in phases 1 and 2 (universal-character-names and line splicing) are reverted; this reversion shall apply before any d-char, r-char, or delimiting parenthesis is identified.
从Phases of translation开始,反斜杠连接下一行发生在阶段2,字符串文字评估发生在阶段3。那么为什么下面的代码之前进行字符串评估?
#include<string>
#include<iostream>
int main() {
std::string s = R"(before\
after)";
std::cout << s;
}
给出:
before\
after
而不是:
beforeafter
Phase 2
- Whenever backslash appears at the end of a line (immediately followed by zero or more whitespace characters other than new-line followed by (since C++23) the newline character), these characters are deleted, combining two physical source lines into one logical source line. [...]
Phase 3
- The source file is decomposed into comments, sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed), and preprocessing tokens, which are the following: a) header names such as or "myfile.h" b) identifiers c) preprocessing numbers d) character and string literals , including user-defined (since C++11) [...]
原始字符串文字 explicitly undo phases 1&2:
If the next character begins a sequence of characters that could be the prefix and initial double quote of a raw string literal, such as R", the next preprocessing token shall be a raw string literal. Between the initial and final double quote characters of the raw string, any transformations performed in phases 1 and 2 (universal-character-names and line splicing) are reverted; this reversion shall apply before any d-char, r-char, or delimiting parenthesis is identified.