2 Lexical conventions [lex]

2.14 Literals [lex.literal]

2.14.8 User-defined literals [lex.ext]

user-defined-literal:
    user-defined-integer-literal
    user-defined-floating-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
user-defined-floating-literal:
    fractional-constant exponent-partopt ud-suffix
    digit-sequence exponent-part ud-suffix
user-defined-string-literal:
    string-literal ud-suffix
user-defined-character-literal:
    character-literal ud-suffix
ud-suffix:
    identifier

If a token matches both user-defined-literal and another literal kind, it is treated as the latter. [ Example: 123_km is a user-defined-literal, but 12LL is an integer-literal.  — end example ] The syntactic non-terminal preceding the ud-suffix in a user-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, the literal-operator-id whose literal suffix identifier is X is looked up in the context of L using the rules for unqualified name lookup ([basic.lookup.unqual]). Let S be the set of declarations found by this lookup. 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 form

operator "" X(nULL)

Otherwise, S shall contain a raw literal operator or a 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 form

operator "" X("n")

Otherwise (S contains a literal operator template), L is treated as a call of the form

operator "" X<'c1', 'c2', ... 'ck'>()

where n is the source character sequence c1c2...ck. [ Note: The sequence c1c2...ck can only contain characters from the basic source character set.  — end note ]

If L is a user-defined-floating-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 form

operator "" X(fL)

Otherwise, S shall contain a raw literal operator or a 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 form

operator "" X("f")

Otherwise (S contains a literal operator template), L is treated as a call of the form

operator "" X<'c1', 'c2', ... 'ck'>()

where f is the source character sequence c1c2...ck. [ Note: The sequence c1c2...ck can only contain characters from the basic source 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). The literal L is treated as a call of the form

operator "" 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 ([over.literal]) whose only parameter has the type of ch and the literal L is treated as a call of the form

operator "" X(ch)

Example:

long double operator "" _w(long double);
std::string operator "" _w(const char16_t*, 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 and user-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 one user-defined-string-literal, all the participating user-defined-string-literals shall have the same ud-suffix and that suffix is applied to the result of the concatenation.

Example:

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 ]

Some identifiers appearing as ud-suffixes are reserved for future standardization ([usrlit.suffix]). A program containing such a ud-suffix is ill-formed, no diagnostic required.