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

109 lines
8.4 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[over.literal]
# 12 Overloading [[over]](./#over)
## 12.6 User-defined literals [over.literal]
[literal-operator-id:](#nt:literal-operator-id "12.6User-defined literals[over.literal]")
operator [*unevaluated-string*](lex.string.uneval#nt:unevaluated-string "5.13.6Unevaluated strings[lex.string.uneval]") [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]")
operator [*user-defined-string-literal*](lex.ext#nt:user-defined-string-literal "5.13.9User-defined literals[lex.ext]")
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4119)
The [*user-defined-string-literal*](lex.ext#nt:user-defined-string-literal "5.13.9User-defined literals[lex.ext]") in a [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") shall have no [*encoding-prefix*](lex.ccon#nt:encoding-prefix "5.13.3Character literals[lex.ccon]")[.](#1.sentence-1)
The [*unevaluated-string*](lex.string.uneval#nt:unevaluated-string "5.13.6Unevaluated strings[lex.string.uneval]") or[*user-defined-string-literal*](lex.ext#nt:user-defined-string-literal "5.13.9User-defined literals[lex.ext]") shall be empty[.](#1.sentence-2)
The [*ud-suffix*](lex.ext#nt:ud-suffix "5.13.9User-defined literals[lex.ext]") of the [*user-defined-string-literal*](lex.ext#nt:user-defined-string-literal "5.13.9User-defined literals[lex.ext]") or
the [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") in a [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") is called a[*literal suffix identifier*](#def:literal,suffix_identifier "12.6User-defined literals[over.literal]")[.](#1.sentence-3)
The first form of [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") is deprecated ([[depr.lit]](depr.lit "D.8Literal operator function declarations using an identifier"))[.](#1.sentence-4)
Some literal suffix identifiers are reserved for future standardization;
see [[usrlit.suffix]](usrlit.suffix "16.4.5.3.6User-defined literal suffixes")[.](#1.sentence-5)
A declaration whose [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") uses
such a literal suffix identifier is ill-formed, no diagnostic required[.](#1.sentence-6)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4133)
A declaration whose [*declarator-id*](dcl.decl.general#nt:declarator-id "9.3.1General[dcl.decl.general]") is a[*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") shall declare a function or function template
that belongs to a namespace (it could be a friend function ([[class.friend]](class.friend "11.8.4Friends"))) or
an explicit instantiation or specialization of a function template[.](#2.sentence-1)
A function declared with a [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") is a [*literal
operator*](#def:literal,operator "12.6User-defined literals[over.literal]")[.](#2.sentence-2)
A function template declared with a [*literal-operator-id*](#nt:literal-operator-id "12.6User-defined literals[over.literal]") is a [*literal operator template*](#def:literal,operator,template "12.6User-defined literals[over.literal]")[.](#2.sentence-3)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4142)
The declaration of a literal operator shall have a[*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6Functions[dcl.fct]") equivalent to one of the following:const char*unsigned 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]](dcl.fct.default "9.3.4.7Default arguments")), the program is
ill-formed[.](#3.sentence-2)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4164)
A [*raw literal operator*](#def:literal,operator,raw "12.6User-defined literals[over.literal]") is a literal operator with a single parameter
whose type is const char*[.](#4.sentence-1)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4168)
A [*numeric literal operator template*](#def:literal,operator,template_numeric "12.6User-defined literals[over.literal]") is a literal operator template whose [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1Preamble[temp.pre]") has a single [*template-parameter*](temp.param#nt:template-parameter "13.2Template parameters[temp.param]") that is a constant template parameter pack ([[temp.variadic]](temp.variadic "13.7.4Variadic templates"))
with element type char[.](#5.sentence-1)
A [*string literal operator template*](#def:literal,operator,template_string "12.6User-defined literals[over.literal]") is a literal operator template whose [*template-parameter-list*](temp.pre#nt:template-parameter-list "13.1Preamble[temp.pre]") comprises
a single [*parameter-declaration*](dcl.fct#nt:parameter-declaration "9.3.4.6Functions[dcl.fct]") that declares a
constant template parameter of class type[.](#5.sentence-2)
The declaration of a literal operator template
shall have an empty [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6Functions[dcl.fct]") and shall declare either a numeric literal operator template
or a string literal operator template[.](#5.sentence-3)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4184)
Literal operators and literal operator templates shall not have C language linkage[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4187)
[*Note [1](#note-1)*:
Literal operators and literal operator templates are usually invoked
implicitly through user-defined literals ([[lex.ext]](lex.ext "5.13.9User-defined literals"))[.](#7.sentence-1)
However, except for
the constraints described above, they are ordinary namespace-scope functions and
function templates[.](#7.sentence-2)
In particular, they are looked up like ordinary functions
and function templates and they follow the same overload resolution rules[.](#7.sentence-3)
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[.](#7.sentence-4)
— *end note*]
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/overloading.tex#L4200)
[*Example [1](#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]](usrlit.suffix "16.4.5.3.6User-defined literal suffixes"), [[lex.ext]](lex.ext "5.13.9User-defined literals"))double operator""_Bq(long double); // OK, does not use the reserved [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") _Bq ([[lex.name]](lex.name "5.11Identifiers"))double operator"" _Bq(long double); // ill-formed, no diagnostic required:// uses the reserved [*identifier*](lex.name#nt:identifier "5.11Identifiers[lex.name]") _Bq ([[lex.name]](lex.name "5.11Identifiers"))float operator " "B(const char*); // error: non-empty [*string-literal*](lex.string#nt:string-literal "5.13.5String literals[lex.string]") string operator ""5X(const char*, std::size_t); // error: invalid literal suffix identifierdouble operator ""_miles(double); // error: invalid [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6Functions[dcl.fct]")template <char...> int operator ""_j(const char*); // error: invalid [*parameter-declaration-clause*](dcl.fct#nt:parameter-declaration-clause "9.3.4.6Functions[dcl.fct]")extern "C" void operator ""_m(long double); // error: C language linkage — *end example*]