cpp/language/user literal

Allows integer, floating-point, character, and string literals to produce objects of user-defined type by defining a user-defined suffix.

Syntax
A user-defined literal is an expression of any of the following forms

@1-4@ user-defined integer literals, such as @5-6@ user-defined floating-point literals, such as @7@ user-defined character literal, such as @8@ user-defined string literal, such as or

If a token matches a user-defined literal syntax and a regular literal syntax, it is assumed to be a regular literal (that is, it's impossible to overload in ).

When the compiler encounters a user-defined literal with, it performs , looking for a function with the name. If the lookup does not find a declaration, the program is ill-formed. Otherwise, @1@ For user-defined integer literals,
 * @a@ if the overload set includes a literal operator with the parameter type, the user-defined literal expression is treated as a function call , where is the literal without ;
 * @b@ otherwise, the overload set must include either, but not both, a raw literal operator or a numeric literal operator template. If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call ;
 * @c@ otherwise, if the overload set includes a numeric literal operator template, the user-defined literal expression is treated as a function call, where .. are the individual characters of and all of them are from the.

@2@ For user-defined floating-point literals,
 * @a@ If the overload set includes a literal operator with the parameter type, the user-defined literal expression is treated as a function call , where is the literal without ;
 * @b@ otherwise, the overload set must include either, but not both, a raw literal operator or a numeric literal operator template. If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call ;
 * @c@ otherwise, if the overload set includes a numeric literal operator template, the user-defined literal expression is treated as a function call, where .. are the individual characters of and all of them are from the.

@3@ For user-defined string literals, let be the literal without :


 * @b@ otherwise, the user-defined literal expression is treated as a function call, where is the length of the string literal, excluding the terminating null character.

@4@ For user-defined character literals, the user-defined literal expression is treated as a function call, where is the literal without.

When string literal concatenation takes place in, user-defined string literals are concatenated as well, and their es are ignored for the purpose of concatenation, except that only one suffix may appear on all concatenated literals:

Literal operators
The function called by a user-defined literal is known as literal operator (or, if it's a template, literal operator template). It is declared just like any other or  at namespace scope (it may also be a friend function, an explicit instantiation or specialization of a function template, or introduced by a using-declaration), except for the following restrictions:

The name of this function can have one of the two forms:

@1@ Declares a literal operator. @2@ Declares a literal operator. This syntax makes it possible to use language keywords and reserved identifiers as es, for example, from the header.

must begin with the underscore : the suffixes that do not begin with the underscore are reserved for the literal operators provided by the standard library. It cannot contain double underscores as well: such suffixes are also reserved.

If the literal operator is a template, it must have an empty parameter list and can have only one template parameter, which must be a non-type template parameter pack with element type (in which case it is known as a numeric literal operator template):

Only the following parameter lists are allowed on literal operators:

@1@ Literal operators with this parameter list are the raw literal operators, used as fallbacks for integer and floating-point user-defined literals (see above) @2@ Literal operators with these parameter lists are the first-choice literal operator for user-defined integer literals @3@ Literal operators with these parameter lists are the first-choice literal operator for user-defined floating-point literals @4-8@ Literal operators with these parameter lists are called by user-defined character literals @9-13@ Literal operators with these parameter lists are called by user-defined string literals

are not allowed.

C is not allowed.

Other than the restrictions above, literal operators and literal operator templates are normal functions (and function templates), they can be declared inline or constexpr, they may have internal or external linkage, they can be called explicitly, their addresses can be taken, etc.

Standard library
The following literal operators are defined in the standard library: