12 KiB
[lex.ext]
5 Lexical conventions [lex]
5.13 Literals [lex.literal]
5.13.9 User-defined literals [lex.ext]
user-defined-literal:
user-defined-integer-literal
user-defined-floating-point-literal
user-defined-string-literal
user-defined-character-literal
user-defined-integer-literal:
decimal-literal ud-suffix
octal-literal ud-suffix
hexadecimal-literal ud-suffix
binary-literal ud-suffix
user-defined-floating-point-literal:
fractional-constant exponent-partopt ud-suffix
digit-sequence exponent-part ud-suffix
hexadecimal-prefix hexadecimal-fractional-constant binary-exponent-part ud-suffix
hexadecimal-prefix hexadecimal-digit-sequence binary-exponent-part ud-suffix
user-defined-string-literal:
string-literal ud-suffix
user-defined-character-literal:
character-literal ud-suffix
If a token matches both user-defined-literal and another literal kind, it is treated as the latter.
[Example 1:
123_km is a user-defined-literal, but 12LL is aninteger-literal.
â end example]
The syntactic non-terminal preceding the ud-suffix in auser-defined-literal is taken to be the longest sequence of characters that could match that non-terminal.
A user-defined-literal is treated as a call to a literal operator or literal operator template ([over.literal]).
To determine the form of this call for a given user-defined-literal L with ud-suffix X, first let S be the set of declarations found by unqualified lookup for the literal-operator-id whose literal suffix identifier is X ([basic.lookup.unqual]).
S shall not be empty.
If L is a user-defined-integer-literal, let n be the literal without its ud-suffix.
If S contains a literal operator with parameter type unsigned long long, the literal L is treated as a call of the formoperator ""X(nULL)
Otherwise, S shall contain a raw literal operator or a numeric literal operator template ([over.literal]) but not both.
If S contains a raw literal operator, the literal L is treated as a call of the formoperator ""X("n")
Otherwise (S contains a numeric literal operator template),L is treated as a call of the formoperator ""X<'c1', 'c2', ... 'ck'>() where n is the source character sequence c1c2...ck.
[Note 1:
The sequencec1c2...ck can only contain characters from the basic character set.
â end note]
If L is a user-defined-floating-point-literal, let f be the literal without its ud-suffix.
If S contains a literal operator with parameter type long double, the literal L is treated as a call of the formoperator ""X(fL)
Otherwise, S shall contain a raw literal operator or a numeric literal operator template ([over.literal]) but not both.
If S contains a raw literal operator, the literal L is treated as a call of the formoperator ""X("f")
Otherwise (S contains a numeric literal operator template),L is treated as a call of the formoperator ""X<'c1', 'c2', ... 'ck'>() where f is the source character sequence c1c2...ck.
[Note 2:
The sequencec1c2...ck can only contain characters from the basic character set.
â end note]
If L is a user-defined-string-literal, let str be the literal without its ud-suffix and let len be the number of code units in str (i.e., its length excluding the terminating null character).
If S contains a literal operator template with a constant template parameter for which str is a well-formed template-argument, the literal L is treated as a call of the formoperator ""X<str>()
Otherwise, the literal L is treated as a call of the formoperator ""X(str, len)
If L is a user-defined-character-literal, let ch be the literal without its ud-suffix.
S shall contain a literal operator whose only parameter has the type of ch and the literal L is treated as a call of the formoperator ""X(ch)
[Example 2: long double operator ""_w(long double); std::string operator ""_w(const char16_t*, std::size_t);unsigned operator ""_w(const char*);int main() {1.2_w; // calls operator ""_w(1.2L)u"one"_w; // calls operator ""_w(u"one", 3)12_w; // calls operator ""_w("12")"two"_w; // error: no applicable literal operator} â end example]
In translation phase 6 ([lex.phases]), adjacent string-literals are concatenated anduser-defined-string-literals are considered string-literals for that purpose.
During concatenation, ud-suffixes are removed and ignored and the concatenation process occurs as described in [lex.string].
At the end of phase 6, if a string-literal is the result of a concatenation involving at least oneuser-defined-string-literal, all the participatinguser-defined-string-literals shall have the same ud-suffix and that suffix is applied to the result of the concatenation.
[Example 3: int main() {L"A" "B" "C"_x; // OK, same as L"ABC"_x"P"_x "Q" "R"_y; // error: two different ud-suffixes} â end example]