User-defined literals (since C++11) - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# User-defined literals (since C++11)

From cppreference.com
< cpp‎ | language

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

### [edit] Syntax

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

decimal-literal ud-suffix | (1) |
octal-literal ud-suffix | (2) |
hex-literal ud-suffix | (3) |
binary-literal ud-suffix | (4) |
fractional-constant exponent-part(optional) ud-suffix | (5) |
digit-sequence exponent-part ud-suffix | (6) |
character-literal ud-suffix | (7) |
string-literal ud-suffix | (8) |

1-4) user-defined integer literals, such as 12_km
5-6) user-defined floating-point literals, such as 0.5_Pa
7) user-defined character literal, such as 'c'_X
8) user-defined string literal, such as "abd"_L or u16"xyz"_M

decimal-literal | - | same as in integer literal, a non-zero decimal digit followed by zero or more decimal digits
octal-literal | - | same as in integer literal, a zero followed by zero or more octal digits
hex-literal | - | same as in integer literal, `0x` or `0X` followed by one or more hexadecimal digits
binary-literal | - | same as in integer literal, `0b` or `0B` followed by one or more binary digits
digit-sequence | - | same as in floating literal, a sequence of decimal digits
fractional-constant | - | same as in floating literal, either a digit-sequence followed by a dot (123.) or an optional digit-sequence followed by a dot and another digit-sequence (1.0 or .12)
exponent-part | - | same as in floating literal, the letter `e` or the letter `E` followed by optional sign, followed by digit-sequence
character-literal | - | same as in character literal
string-literal | - | same as in string literal, including raw string literals
ud-suffix | - | an identifier, introduced by a literal operator or a literal operator template declaration (see below). All ud-suffixes introduced by a program must begin with the underscore character `_`. The standard library ud-suffixes do not begin with underscores.

In the integer and floating-point digit sequences, optional separators `'` are allowed between any two digits and are ignored | (since C++14)

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 LL in 123LL)

When the compiler encounters a user-defined literal with ud-suffix `X`, it performs unqualified name lookup, looking for a function with the name `operator "" X`. 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 unsigned long long, the user-defined literal expression is treated as a function call operator "" X(nULL), where n is the literal without ud-suffix
b) otherwise, the overload set must include either, but not both, a raw literal operator or a raw literal operator template. If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call operator "" X("n")
c) otherwise, if the overload set includes a raw literal operator template, the user-defined literal expression is treated as a function call operator "" X<'c1', 'c2', 'c3'..., 'ck'>(), where c1..ck are the individual characters of `n`.
2) For user-defined floating-point literals,
a) If the overload set includes a literal operator with the parameter type long double, the user-defined literal expression is treated as a function call operator "" X(fL), where `f` is the literal without ud-suffix
b) otherwise, the overload set must include either, but not both, a raw literal operator or a raw literal operator template. If the overload set includes a raw literal operator, the user-defined literal expression is treated as a function call operator "" X("f")
c) otherwise, if the overload set includes a raw literal operator template, the user-defined literal expression is treated as a function call operator "" X<'c1', 'c2', 'c3'..., 'ck'>(), where c1..ck are the individual characters of `f`.
3) For user-defined string literals, the user-defined literal expression is treated as a function call operator "" X (str, len), where `str` is the literal without ud-suffix and `len` is its length excluding the terminating null character
4) For user-defined character literals, the user-defined literal expression is treated as a function call operator "" X (ch), where `ch` is the literal without ud-suffix

    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
    }

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

    int main() {
        L"A" "B" "C"_x; // OK: same as L"ABC"_x
        "P"_x "Q" "R"_y;// error: two different ud-suffixes (_x and _y)
    }

### [edit] 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 function or function template 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:

`operator` `""` identifier |  |
`operator` user-defined-string-literal |  |

identifier | - | the identifier to use as the ud-suffix for the user-defined literals that will call this function. 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.
user-defined-string-literal | - | the character sequence `""` followed, without a space, by the character sequence that becomes the ud-suffix. This special syntax makes it possible to use language keywords and reserved identifiers as ud-suffixes, and is used by the declaration of operator ""if from the header <complex>.

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 char

    template <char...> double operator "" _x();

Only the following parameter lists are allowed on literal operators :

`(` const char * `)` | (1) |
`(` unsigned long long int `)` | (2) |
`(` long double `)` | (3) |
`(` char `)` | (4) |
`(` wchar_t `)` | (5) |
`(` char16_t `)` | (6) |
`(` char32_t `)` | (7) |
`(` const char * `,` std::size_t `)` | (8) |
`(` const wchar_t * `,` std::size_t `)` | (9) |
`(` const char16_t * `,` std::size_t `)` | (10) |
`(` const char32_t * `,` std::size_t `)` | (11) |

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-7) Literal operators with these parameter lists are called by user-defined character literals
8-11) Literal operators with these parameter lists are called by user-defined string literals

Default arguments are not allowed

C language linkage 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.

    void operator "" _km(long double); // OK, will be called for 1.0_km
    std::string operator "" _i18n(const char*, std::size_t); // OK
    template <char...> double operator "" _\u03C0(); // OK: (\u03C0 is π)
    float operator ""_e(const char*); // OK

    float operator ""E(const char*); // error: suffix must begin with underscore
    double operator"" _E(double); // error: all names that begin with underscore
                                  // followed by uppercase letter are reserved
    double operator""_E(double); // OK: even though _E is reserved ""_E is allowed

### [edit] Notes

Since the introduction of user-defined literals, the code that uses format macro constants for fixed-width integer types with no space after the preceding string literal became invalid: std::printf("%"PRId64"\n",INT64_MIN); has to be replaced by std::printf("%" PRId64"\n",INT64_MIN);

### [edit] Examples

Run this code

    #include <iostream>

    // used as conversion
    constexpr long double operator"" _deg ( long double deg )
    {
        return deg*3.141592/180;
    }

    // used with custom type
    struct mytype
    {
        mytype ( unsigned long long m):m(m){}
        unsigned long long m;
    };
    mytype operator"" _mytype ( unsigned long long n )
    {
        return mytype(n);
    }

    // used for side-effects
    void operator"" _print ( const char* str )
    {
        std::cout << str;
    }

    int main(){
        double x = 90.0_deg;
        std::cout << std::fixed << x << '\n';
        mytype y = 123_mytype;
        std::cout << y.m << '\n';
        0x123ABC_print;
    }

Output:

    1.570796
    123
    0x123ABC

### [edit] Standard library

The following literal operators are defined in the standard library

Defined in inline namespace `std::literals::complex_literals`
operator""ifoperator""ioperator""il (C++14) | A std::complex literal representing pure imaginary number (function) [edit]
Defined in inline namespace `std::literals::chrono_literals`
operator""h (C++14) | A std::chrono::duration literal representing hours (function) [edit]
operator""min (C++14) | A std::chrono::duration literal representing minutes (function) [edit]
operator""s (C++14) | A std::chrono::duration literal representing seconds (function) [edit]
operator""ms (C++14) | A std::chrono::duration literal representing milliseconds (function) [edit]
operator""us (C++14) | A std::chrono::duration literal representing microseconds (function) [edit]
operator""ns (C++14) | A std::chrono::duration literal representing nanoseconds (function) [edit]
Defined in inline namespace `std::literals::string_literals`
operator""s (C++14) | Converts a character array literal to `basic_string` (function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/language/user_literal&oldid=71459"