Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Raw character literal

I don't know if I've missed something or it really doesn't exists. In the C++11 standard the Raw string literals were added:

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

But all my attempts to use a Raw character literal have failed:

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

The second attempt is considered a multi-character constant! The only way I've found to use something similar to Raw character literal is:

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

But I don't like this notation (dereferencing a string literal in order to store a copy of the first element) because is kind of confusing.

Well, what's the question?

  • Did the Raw character literals exists? (I've not found nothing about them so I'm nearly sure that they don't)
    • If they exists: How I should write a Raw character literal?
    • If they don't exist: Why? Is there a reason to add Raw string literals but AVOID to add Raw character literals?
like image 232
PaperBirdMaster Avatar asked May 27 '15 11:05

PaperBirdMaster


1 Answers

The proposal that introduced raw string literals in C++11 is, as far as I can tell, N2442 - Raw and Unicode String Literals; Unified Proposal. It is based upon N2146 - Raw String Literals (Revision 1) by Beman Dawes, which contains a section about raw character literals:

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.

Unfortunately, I cannot find any discussion in the meeting minutes that mention any of the related proposals. It is likely though that the reason mentioned first paragraph lead to the current situation.

like image 101
dyp Avatar answered Oct 22 '22 05:10

dyp