原始字符文字

Raw character literal

我不知道我是漏掉了什么还是真的不存在。在 C++11 标准中添加了 Raw string literals

string s = "\w\\\w"; // I hope I got that right
string s = R"(\w\\w)";  // I'm pretty sure I got that right

但是我所有使用原始字符文字的尝试都失败了:

constexpr char bslash = R('\'); // error: missing terminating ' character
constexpr char bslash = R'(\)'; // error: 'R' was not declared in this scope

第二次尝试被认为是多字符常量!我发现使用类似于原始字符文字的唯一方法是:

constexpr char slash = *R"(\)"; // All Ok.

但我不喜欢这种表示法(取消引用字符串文字以存储第一个元素的副本),因为它有点令人困惑。

嗯,问题是什么?

据我所知,在 C++11 中引入原始字符串文字的提议是 N2442 - Raw and Unicode String Literals; Unified Proposal. It is based upon N2146 - Raw String Literals (Revision 1) Beman Dawes,其中包含关于原始字符文字:

As a deliberate design choice, raw character (as opposed to string) literals are not proposed because there is no apparent need; escape sequences do not pose the same practical problems in character literals that they do in string literals.

The arguments in favor of raw character literals are symmetry and error-reduction. Knowing that raw string-literals are allowed, programmers are likely to assume raw character-literals are also available. Indeed, a committee member inadvertently made that assumption when reading a draft of this paper. Although the resulting error is easy to fix, there is the argument that it is better to eliminate the possibility of the error by providing raw character-literals in the first place.

I will be happy to provide proposed wording if the committee desires to add raw character literals.

遗憾的是,我在会议记录中找不到任何提及任何相关提案的讨论。很可能是第一段提到的原因导致现在的情况。