Files
cppdraft_translate/cppdraft/over/literal.md
2025-10-25 03:02:53 +03:00

8.4 KiB

[over.literal]

12 Overloading [over]

12.6 User-defined literals [over.literal]

literal-operator-id:
operator unevaluated-string identifier
operator user-defined-string-literal

1

#

The user-defined-string-literal in a literal-operator-id shall have no encoding-prefix.

The unevaluated-string oruser-defined-string-literal shall be empty.

The ud-suffix of the user-defined-string-literal or the identifier in a literal-operator-id is called aliteral suffix identifier.

The first form of literal-operator-id is deprecated ([depr.lit]).

Some literal suffix identifiers are reserved for future standardization; see [usrlit.suffix].

A declaration whose literal-operator-id uses such a literal suffix identifier is ill-formed, no diagnostic required.

2

#

A declaration whose declarator-id is aliteral-operator-id shall declare a function or function template that belongs to a namespace (it could be a friend function ([class.friend])) or an explicit instantiation or specialization of a function template.

A function declared with a literal-operator-id is a literal operator.

A function template declared with a literal-operator-id is a literal operator template.

3

#

The declaration of a literal operator shall have aparameter-declaration-clause equivalent to one of the following:const charunsigned long long intlong doublecharwchar_tchar8_tchar16_tchar32_tconst char, std::size_tconst wchar_t*, std::size_tconst char8_t*, std::size_tconst char16_t*, std::size_tconst char32_t*, std::size_t

If a parameter has a default argument ([dcl.fct.default]), the program is ill-formed.

4

#

A raw literal operator is a literal operator with a single parameter whose type is const char*.

5

#

A numeric literal operator template is a literal operator template whose template-parameter-list has a single template-parameter that is a constant template parameter pack ([temp.variadic]) with element type char.

A string literal operator template is a literal operator template whose template-parameter-list comprises a single parameter-declaration that declares a constant template parameter of class type.

The declaration of a literal operator template shall have an empty parameter-declaration-clause and shall declare either a numeric literal operator template or a string literal operator template.

6

#

Literal operators and literal operator templates shall not have C language linkage.

7

#

[Note 1:

Literal operators and literal operator templates are usually invoked implicitly through user-defined literals ([lex.ext]).

However, except for the constraints described above, they are ordinary namespace-scope functions and function templates.

In particular, they are looked up like ordinary functions and function templates and they follow the same overload resolution rules.

Also, they can be declared inline or constexpr, they can have internal, module, or external linkage, they can be called explicitly, their addresses can be taken, etc.

— end note]

8

#

[Example 1: void operator ""_km(long double); // OK string operator "" i18n(const char*, std::size_t); // OK, deprecatedtemplate <char...> double operator ""\u03C0(); // OK, UCN for lowercase pifloat operator ""_e(const char*); // OKfloat operator ""E(const char*); // ill-formed, no diagnostic required:// reserved literal suffix ([usrlit.suffix], [lex.ext])double operator""_Bq(long double); // OK, does not use the reserved identifier _Bq ([lex.name])double operator"" _Bq(long double); // ill-formed, no diagnostic required:// uses the reserved identifier _Bq ([lex.name])float operator " "B(const char*); // error: non-empty string-literal string operator ""5X(const char*, std::size_t); // error: invalid literal suffix identifierdouble operator ""_miles(double); // error: invalid parameter-declaration-clausetemplate <char...> int operator ""_j(const char*); // error: invalid parameter-declaration-clauseextern "C" void operator ""_m(long double); // error: C language linkage — end example]