Custom String Literals Vs. Other user literals

Let's look at the following quote from the C ++ 11 standard ( project N3376 , to be precise):

(2.14.8.5)

If L is a user-defined literal - string, let str be a literal without its ud-su ffi x and len the number of code units in str (i.e. its length, excluding the finite null character). The literal L is considered as a call of the form

operator "" X (str , len ) 

While for all other types of custom literals (floating point, integer, character), the length is never passed, even if the literal itself is passed as a string. For instance:

 42_zzz; // calls operator "" _zzz("42") and not operator "" _zzz("42", 2) 

Why is there such a difference between string and non-string user literals? Or should I say why the implementation is passed len to UD string literals? Length, as with other literals, can be inferred by null termination. What am I missing?

+6
source share
2 answers

For a string literal, it’s reasonable enough for the null character to be embedded in the string sequence, for example, "a\0b" . To allow an implementation to use the entire string literal, even if there is a built-in null character, it needs to know the length of the literal. Other forms for user literals cannot contain embedded null characters.

+8
source

Lines always end in C / C ++, but this never means that they cannot contain the embedded character \0 , you can have "1234\05678" , and although this line has a null termination, it contains an additional "\ 0 "in it.

+6
source

All Articles