[over] (original) (raw)
12 Overloading [over]
12.1 Preamble [over.pre]
[Note 1:
Each of two or more entities with the same name in the same scope, which must be functions or function templates, is commonly called an “overload”.
— _end note_]
When a function is named in a call, which function declaration is being referenced and the validity of the call are determined by comparing the types of the arguments at the point of use with the types of the parameters in the declarations in the overload set.
This function selection process is calledoverload resolution and is defined in [over.match].
[Example 1: double abs(double);int abs(int); abs(1); abs(1.0); — _end example_]
12.2 Overload resolution [over.match]
12.2.1 General [over.match.general]
Overload resolution is a mechanism for selecting the best function to call given a list of expressions that are to be the arguments of the call and a set ofcandidate functionsthat can be called based on the context of the call.
The selection criteria for the best function are the number of arguments, how well the arguments match the parameter-type-list of the candidate function, how well (for non-static member functions) the object matches the object parameter, and certain other properties of the candidate function.
[Note 1:
The function selected by overload resolution is not guaranteed to be appropriate for the context.
Other restrictions, such as the accessibility of the function, can make its use in the calling context ill-formed.
— _end note_]
Overload resolution selects the function to call in seven distinct contexts within the language:
- invocation of a function named in the function call syntax;
- invocation of a function call operator, a pointer-to-function conversion function, a reference-to-pointer-to-function conversion function, or a reference-to-function conversion function on a class object named in the function call syntax ([over.call.object]);
- invocation of the operator referenced in an expression ([over.match.oper]);
- invocation of a constructor for default- or direct-initialization ([dcl.init]) of a class object ([over.match.ctor]);
- invocation of a user-defined conversion forcopy-initialization of a class object ([over.match.copy]);
- invocation of a conversion function for initialization of an object of a non-class type from an expression of class type ([over.match.conv]); and
- invocation of a conversion function for conversion in which a reference ([dcl.init.ref]) will be directly bound.
Each of these contexts defines the set of candidate functions and the list of arguments in its own unique way.
But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases:
- First, a subset of the candidate functions (those that have the proper number of arguments and meet certain other conditions) is selected to form a set ofviable functions ([over.match.viable]).
- Then the best viable function is selected based on theimplicit conversion sequences needed to match each argument to the corresponding parameter of each viable function.
If a best viable function exists and is unique, overload resolution succeeds and produces it as the result.
Otherwise overload resolution fails and the invocation is ill-formed.
When overload resolution succeeds, and the best viable function is not accessible in the context in which it is used, the program is ill-formed.
Overload resolution results in a usable candidateif overload resolution succeeds and the selected candidate is either not a function ([over.built]), or is a function that is not deleted and is accessible from the context in which overload resolution was performed.
12.2.2 Candidate functions and argument lists [over.match.funcs]
12.2.2.1 General [over.match.funcs.general]
The subclauses of [over.match.funcs] describe the set of candidate functions and the argument list submitted to overload resolution in each context in which overload resolution is used.
The source transformations and constructions defined in these subclauses are only for the purpose of describing the overload resolution process.
An implementation is not required to use such transformations and constructions.
The set of candidate functions can contain both member and non-member functions to be resolved against the same argument list.
If a member function is
- an implicit object member function that is not a constructor, or
- a static member function and the argument list includes an implied object argument,
it is considered to have an extra first parameter, called the implicit object parameter, which represents the object for which the member function has been called.
Similarly, when appropriate, the context can construct an argument list that contains animplied object argumentas the first argument in the list to denote the object to be operated on.
For implicit object member functions, the type of the implicit object parameter is
- “lvalue reference to cv X” for functions declared without a ref-qualifier or with the& ref-qualifier
- “rvalue reference to cv X” for functions declared with the&& ref-qualifier
whereXis the class of which the function is a direct member andcvis the cv-qualification on the member function declaration.
[Example 1:
For aconstmember function of classX, the extra parameter is assumed to have type “lvalue reference toconst X”.
— _end example_]
For conversion functions that are implicit object member functions, the function is considered to be a member of the class of the implied object argument for the purpose of defining the type of the implicit object parameter.
For non-conversion functions that are implicit object member functions nominated by a using-declarationin a derived class, the function is considered to be a member of the derived class for the purpose of defining the type of the implicit object parameter.
For static member functions, the implicit object parameter is considered to match any object (since if the function is selected, the object is discarded).
[Note 1:
No actual type is established for the implicit object parameter of a static member function, and no attempt will be made to determine a conversion sequence for that parameter ([over.match.best]).
— _end note_]
During overload resolution, the implied object argument is indistinguishable from other arguments.
The implicit object parameter, however, retains its identity since no user-defined conversions can be applied to achieve a type match with it.
For implicit object member functions declared without a ref-qualifier, even if the implicit object parameter is not const-qualified, an rvalue can be bound to the parameter as long as in all other respects the argument can be converted to the type of the implicit object parameter.
[Note 2:
The fact that such an argument is an rvalue does not affect the ranking of implicit conversion sequences.
— _end note_]
Because other than in list-initialization only one user-defined conversion is allowed in an implicit conversion sequence, special rules apply when selecting the best user-defined conversion ([over.match.best], [over.best.ics]).
[Example 2: class T { public: T();};class C : T { public: C(int);}; T a = 1; — _end example_]
In each case where conversion functions of a class S are considered for initializing an object or reference of type T, the candidate functions include the result of a search for the conversion-function-id operator Tin S.
[Note 3:
This search can find a specialization of a conversion function template ([basic.lookup]).
— _end note_]
Each such case also defines sets of permissible typesfor explicit and non-explicit conversion functions; each (non-template) conversion function that
- is a non-hidden member of S,
- yields a permissible type, and,
- for the former set, is non-explicit
is also a candidate function.
If initializing an object, for any permissible type cv U, any_cv2_ U, cv2 U&, or cv2 U&&is also a permissible type.
If the set of permissible types for explicit conversion functions is empty, any candidates that are explicit are discarded.
In each case where a candidate is a function template, candidate function template specializations are generated using template argument deduction ([temp.over], [temp.deduct]).
If a constructor template or conversion function template has an explicit-specifierwhose constant-expression is value-dependent ([temp.dep]), template argument deduction is performed first and then, if the context admits only candidates that are not explicit and the generated specialization is explicit ([dcl.fct.spec]), it will be removed from the candidate set.
Those candidates are then handled as candidate functions in the usual way.99
A given name can refer to, or a conversion can consider, one or more function templates as well as a set of non-template functions.
In such a case, the candidate functions generated from each function template are combined with the set of non-template candidate functions.
A defaulted move special member function ([class.copy.ctor], [class.copy.assign]) that is defined as deleted is excluded from the set of candidate functions in all contexts.
A constructor inherited from class type C ([class.inhctor.init]) that has a first parameter of type “reference to cv1 P” (including such a constructor instantiated from a template) is excluded from the set of candidate functions when constructing an object of type cv2 Dif the argument list has exactly one argument andC is reference-related to P andP is reference-related to D.
[Example 3: struct A { A(); A(A &&); template<typename T> A(T &&); };struct B : A { using A::A; B(const B &); B(B &&) = default; struct X { X(X &&) = delete; } x;};extern B b1; B b2 = static_cast<B&&>(b1); struct C { operator B&&(); }; B b3 = C(); — _end example_]
12.2.2.2 Function call syntax [over.match.call]
12.2.2.2.1 General [over.match.call.general]
If the postfix-expression denotes an object of class type, overload resolution is applied as specified in [over.call.object].
If the postfix-expression is the address of an overload set, overload resolution is applied using that set as described above.
[Note 1:
No implied object argument is added in this case.
— _end note_]
If the function selected by overload resolution is an implicit object member function, the program is ill-formed.
[Note 2:
The resolution of the address of an overload set in other contexts is described in [over.over].
— _end note_]
12.2.2.2.2 Call to named function [over.call.func]
Of interest in [over.call.func] are only those function calls in which the postfix-expressionultimately contains an id-expression that denotes one or more functions.
Such apostfix-expression, perhaps nested arbitrarily deep in parentheses, has one of the following forms:
These represent two syntactic subcategories of function calls: qualified function calls and unqualified function calls.
In qualified function calls, the function is named by an id-expressionpreceded by an -> or . operator.
Since the constructA->Bis generally equivalent to(*A).B, the rest of [over] assumes, without loss of generality, that all member function calls have been normalized to the form that uses an object and the.operator.
Furthermore, [over] assumes that thepostfix-expressionthat is the left operand of the.operator has type “cv T” whereTdenotes a class.100
The function declarations found by name lookup ([class.member.lookup]) constitute the set of candidate functions.
The argument list is theexpression-listin the call augmented by the addition of the left operand of the.operator in the normalized member function call as the implied object argument ([over.match.funcs]).
In unqualified function calls, the function is named by aprimary-expression.
The function declarations found by name lookup ([basic.lookup]) constitute the set of candidate functions.
Because of the rules for name lookup, the set of candidate functions consists either entirely of non-member functions or entirely of member functions of some classT.
In the former case or if the primary-expression is the address of an overload set, the argument list is the same as theexpression-listin the call.
Otherwise, the argument list is theexpression-listin the call augmented by the addition of an implied object argument as in a qualified function call.
If the current class is, or is derived from, T, and the keywordthis ([expr.prim.this]) refers to it,
- if the unqualified function call appears in a precondition assertion of a constructor or a postcondition assertion of a destructor and overload resolution selects a non-static member function, the call is ill-formed;
- otherwise, the implied object argument is(*this).
Otherwise,
- if overload resolution selects a non-static member function, the call is ill-formed;
- otherwise, a contrived object of typeTbecomes the implied object argument.101
[Example 1: struct C { bool a();void b() { a(); } void c(this const C&); void c() &; static void c(int = 0); void d() { c(); (C::c)(); (&(C::c))(); (&C::c)(C{}); (&C::c)(*this); (&C::c)(); } void f(this const C&);void g() const { f(); f(*this); this->f(); } static void h() { f(); f(C{}); C{}.f(); } void k(this int);operator int() const;void m(this const C& c) { c.k(); } C() pre(a()) pre(this->a()) post(a()); ~C() pre(a()) post(a()) post(this->a()); }; — _end example_]
12.2.2.2.3 Call to object of class type [over.call.object]
If the postfix-expression Ein the function call syntax evaluates to a class object of type “cv T”, then the set of candidate functions includes at least the function call operators of T.
The function call operators of Tare the results of a search for the name operator()in the scope of T.
In addition, for each non-explicit conversion function declared in T of the form
where the optionalcv-qualifier-seqis the same cv-qualification as, or a greater cv-qualification than,cv, and whereconversion-type-iddenotes the type “pointer to function of () returning R”, or the type “reference to pointer to function of () returning R”, or the type “reference to function of () returning R”, a surrogate call function with the unique name_call-function_and having the form
R call-function ( conversion-type-id F, P a, …, P a) { return F (a, …, a); }
is also considered as a candidate function.
Similarly, surrogate call functions are added to the set of candidate functions for each non-explicit conversion function declared in a base class ofTprovided the function is not hidden withinTby another intervening declaration.102
The argument list submitted to overload resolution consists of the argument expressions present in the function call syntax preceded by the implied object argument(E).
[Note 1:
When comparing the call against the function call operators, the implied object argument is compared against the object parameter of the function call operator.
When comparing the call against a surrogate call function, the implied object argument is compared against the first parameter of the surrogate call function.
— _end note_]
[Example 1: int f1(int);int f2(float);typedef int (*fp1)(int);typedef int (*fp2)(float);struct A { operator fp1() { return f1; } operator fp2() { return f2; } } a;int i = a(1); — _end example_]
12.2.2.3 Operators in expressions [over.match.oper]
If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to [expr.compound].
[Note 1:
Because.,.*, and::cannot be overloaded, these operators are always built-in operators interpreted according to[expr.compound].
?:cannot be overloaded, but the rules in this subclause are used to determine the conversions to be applied to the second and third operands when they have class or enumeration type ([expr.cond]).
— _end note_]
[Example 1: struct String { String (const String&); String (const char*);operator const char* ();}; String operator + (const String&, const String&);void f() { const char* p= "one" + "two"; int I = 1 + 1; } — _end example_]
If either operand has a type that is a class or an enumeration, a user-defined operator function can be declared that implements this operator or a user-defined conversion can be necessary to convert the operand to a type that is appropriate for a built-in operator.
In this case, overload resolution is used to determine which operator function or built-in operator is to be invoked to implement the operator.
Therefore, the operator notation is first transformed to the equivalent function-call notation as summarized in Table 18(where @ denotes one of the operators covered in the specified subclause).
However, the operands are sequenced in the order prescribed for the built-in operator ([expr.compound]).
Table 18 — Relationship between operator and function call notation [tab:over.match.oper]
🔗Subclause | Expression | As member function | As non-member function |
---|---|---|---|
🔗[over.unary] | @a | (a).operator@ ( ) | operator@(a) |
🔗[over.binary] | a@b | (a).operator@ (b) | operator@(a, b) |
🔗[over.assign] | a=b | (a).operator= (b) | |
🔗[over.sub] | a[b] | (a).operator[](b) | |
🔗[over.ref] | a-> | (a).operator->( ) | |
🔗[over.inc] | a@ | (a).operator@ (0) | operator@(a, 0) |
For a unary operator @with an operand of type cv1 T1, and for a binary operator @with a left operand of type cv1 T1and a right operand of type cv2 T2, four sets of candidate functions, designatedmember candidates,non-member candidates,built-in candidates, andrewritten candidates, are constructed as follows:
- If T1 is a complete class type or a class currently being defined, the set of member candidates is the result of a search foroperator@ in the scope of T1; otherwise, the set of member candidates is empty.
- For the operators =, [], or ->, the set of non-member candidates is empty; otherwise, it includes the result of unqualified lookup foroperator@in the rewritten function call ([basic.lookup.unqual], [basic.lookup.argdep]), ignoring all member functions.
However, if no operand has a class type, only those non-member functions in the lookup set that have a first parameter of typeT1or “reference to cv T1”, whenT1is an enumeration type, or (if there is a right operand) a second parameter of typeT2or “reference to cv T2”, whenT2is an enumeration type, are candidate functions. - For the operator,, the unary operator&, or the operator->, the built-in candidates set is empty.
For all other operators, the built-in candidates include all of the candidate operator functions defined in [over.built] that, compared to the given operator,- have the same operator name, and
- accept the same number of operands, and
- accept operand types to which the given operand or operands can be converted according to [over.best.ics], and
- do not have the same parameter-type-list as any non-member candidate or rewritten non-member candidate that is not a function template specialization.
- The rewritten candidate set is determined as follows:
- For the relational ([expr.rel]) operators, the rewritten candidates include all non-rewritten candidates for the expression x <=> y.
- For the relational ([expr.rel]) and three-way comparison ([expr.spaceship]) operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expressiony <=> x.
- For the != operator ([expr.eq]), the rewritten candidates include all non-rewritten candidates for the expression x == ythat are rewrite targets with first operand x (see below).
- For the equality operators, the rewritten candidates also include a synthesized candidate, with the order of the two parameters reversed, for each non-rewritten candidate for the expression y == xthat is a rewrite target with first operand y.
- For all other operators, the rewritten candidate set is empty.
[Note 2:
A candidate synthesized from a member candidate has its object parameter as the second parameter, thus implicit conversions are considered for the first, but not for the second, parameter.
— _end note_]
A non-template function or function template F named operator==is a rewrite target with first operand ounless a search for the name operator!= in the scope Sfrom the instantiation context of the operator expression finds a function or function template that would correspond ([basic.scope.scope]) to Fif its name were operator==, where S is the scope of the class type of oif F is a class member, and the namespace scope of which F is a member otherwise.
A function template specialization named operator== is a rewrite target if its function template is a rewrite target.
[Example 2: struct A {};template<typename T> bool operator==(A, T); bool a1 = 0 == A(); template<typename T> bool operator!=(A, T);bool a2 = 0 == A(); struct B { bool operator==(const B&); };struct C : B { C(); C(B);bool operator!=(const B&); };bool c1 = B() == C(); bool c2 = C() == B(); struct D {};template<typename T> bool operator==(D, T); inline namespace N { template<typename T> bool operator!=(D, T); } bool d1 = 0 == D(); — _end example_]
For the first parameter of the built-in assignment operators, only standard conversion sequences ([over.ics.scs]) are considered.
For all other operators, no such restrictions apply.
The set of candidate functions for overload resolution for some operator @is the union of the member candidates, the non-member candidates, the built-in candidates, and the rewritten candidates for that operator @.
The argument list contains all of the operands of the operator.
The best function from the set of candidate functions is selected according to [over.match.viable]and [over.match.best].103
[Example 3: struct A { operator int();}; A operator+(const A&, const A&);void m() { A a, b; a + b; } — _end example_]
If a rewritten operator<=> candidate is selected by overload resolution for an operator @,x @ yis interpreted as0 @ (y <=> x)if the selected candidate is a synthesized candidate with reversed order of parameters, or (x <=> y) @ 0 otherwise, using the selected rewritten operator<=> candidate.
Rewritten candidates for the operator @are not considered in the context of the resulting expression.
If a rewritten operator== candidate is selected by overload resolution for an operator @, its return type shall be cv bool, andx @ y is interpreted as:
- if @ is !=and the selected candidate is a synthesized candidate with reversed order of parameters,!(y == x),
- otherwise, if @ is !=,!(x == y),
- otherwise (when @ is ==),y == x,
in each case using the selected rewritten operator== candidate.
If a built-in candidate is selected by overload resolution, the operands of class type are converted to the types of the corresponding parameters of the selected operation function, except that the second standard conversion sequence of a user-defined conversion sequence is not applied.
Then the operator is treated as the corresponding built-in operator and interpreted according to [expr.compound].
[Example 4: struct X { operator double();};struct Y { operator int*();};int *a = Y() + 100.0; int *b = Y() + X(); — _end example_]
The second operand of operator->is ignored in selecting anoperator->function, and is not an argument when theoperator->function is called.
Whenoperator->returns, the operator->is applied to the value returned, with the original second operand.104
If the operator is the operator,, the unary operator&, or the operator->, and there are no viable functions, then the operator is assumed to be the built-in operator and interpreted according to[expr.compound].
[Note 3:
The lookup rules for operators in expressions are different than the lookup rules for operator function names in a function call, as shown in the following example:struct A { };void operator + (A, A);struct B { void operator + (B);void f ();}; A a;void B::f() { operator+ (a,a); a + a; }
— _end note_]
12.2.2.4 Initialization by constructor [over.match.ctor]
When objects of class type are direct-initialized, copy-initialized from an expression of the same or a derived class type ([dcl.init]), or default-initialized, overload resolution selects the constructor.
For direct-initialization or default-initialization (including default-initialization in the context of copy-list-initialization), the candidate functions are all the constructors of the class of the object being initialized.
Otherwise, the candidate functions are all the non-explicit constructors ([class.conv.ctor]) of that class.
For default-initialization in the context of copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.
12.2.2.5 Copy-initialization of class by user-defined conversion [over.match.copy]
Under the conditions specified in [dcl.init], as part of a copy-initialization of an object of class type, a user-defined conversion can be invoked to convert an initializer expression to the type of the object being initialized.
Overload resolution is used to select the user-defined conversion to be invoked.
[Note 1:
The conversion performed for indirect binding to a reference to a possibly cv-qualified class type is determined in terms of a corresponding non-reference copy-initialization.
— _end note_]
Assuming that “cv1 T” is the type of the object being initialized, withTa class type, the candidate functions are selected as follows:
- When the type of the initializer expression is a class type “cv S”, conversion functions are considered.
The permissible types for non-explicit conversion functions areT and any class derived from T.
When initializing a temporary object ([class.mem]) to be bound to the first parameter of a constructor where the parameter is of type “reference to cv2 T” and the constructor is called with a single argument in the context of direct-initialization of an object of type “cv3 T”, the permissible types for explicit conversion functions are the same; otherwise there are none.
In both cases, the argument list has one argument, which is the initializer expression.
[Note 2:
This argument will be compared against the first parameter of the constructors and against the object parameter of the conversion functions.
— _end note_]
12.2.2.6 Initialization by conversion function [over.match.conv]
Under the conditions specified in [dcl.init], as part of an initialization of an object of non-class type, a conversion function can be invoked to convert an initializer expression of class type to the type of the object being initialized.
Overload resolution is used to select the conversion function to be invoked.
Assuming that “cv T” is the type of the object being initialized, the candidate functions are selected as follows:
- The permissible types for non-explicit conversion functions are those that can be converted to type Tvia a standard conversion sequence ([over.ics.scs]).
For direct-initialization, the permissible types for explicit conversion functions are those that can be converted to type Twith a (possibly trivial) qualification conversion ([conv.qual]); otherwise there are none.
The argument list has one argument, which is the initializer expression.
[Note 1:
This argument will be compared against the object parameter of the conversion functions.
— _end note_]
12.2.2.7 Initialization by conversion function for direct reference binding [over.match.ref]
Under the conditions specified in [dcl.init.ref], a reference can be bound directly to the result of applying a conversion function to an initializer expression.
Overload resolution is used to select the conversion function to be invoked.
Assuming that “reference to cv1 T” is the type of the reference being initialized, the candidate functions are selected as follows:
- Let R be a set of types including
- “lvalue reference to cv2 T2” (when converting to an lvalue) and
- “cv2 T2” and “rvalue reference to cv2 T2” (when converting to an rvalue or an lvalue of function type)
for any T2.
The permissible types for non-explicit conversion functions are the members of Rwhere “cv1 T” is reference-compatible ([dcl.init.ref]) with “cv2 T2”.
For direct-initialization, the permissible types for explicit conversion functions are the members of Rwhere T2 can be converted to type Twith a (possibly trivial) qualification conversion ([conv.qual]); otherwise there are none.
The argument list has one argument, which is the initializer expression.
[Note 1:
This argument will be compared against the object parameter of the conversion functions.
— _end note_]
12.2.2.8 Initialization by list-initialization [over.match.list]
When objects of non-aggregate class type T are list-initialized such that [dcl.init.list] specifies that overload resolution is performed according to the rules in this subclause or when forming a list-initialization sequence according to [over.ics.list], overload resolution selects the constructor in two phases:
- If the initializer list is not empty or T has no default constructor, overload resolution is first performed where the candidate functions are the initializer-list constructors ([dcl.init.list]) of the class T and the argument list consists of the initializer list as a single argument.
- Otherwise, or if no viable initializer-list constructor is found, overload resolution is performed again, where the candidate functions are all the constructors of the class T and the argument list consists of the elements of the initializer list.
In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.
[Note 1:
This differs from other situations ([over.match.ctor], [over.match.copy]), where only non-explicit constructors are considered for copy-initialization.
This restriction only applies if this initialization is part of the final result of overload resolution.
— _end note_]
12.2.2.9 Class template argument deduction [over.match.class.deduct]
When resolving a placeholder for a deduced class type ([dcl.type.class.deduct]) where the template-name names a primary class template C, a set of functions and function templates, called the guides of C, is formed comprising:
- If C is defined, for each constructor of C, a function template with the following properties:
- The template parameters are the template parameters of Cfollowed by the template parameters (including default template arguments) of the constructor, if any.
- The associated constraints ([temp.constr.decl]) are the conjunction of the associated constraints of C and the associated constraints of the constructor, if any.
- The return type is the class template specialization designated by Cand template arguments corresponding to the template parameters of C.
- If Cis not defined or does not declare any constructors, an additional function template derived as above from a hypothetical constructor C().
- An additional function template derived as above from a hypothetical constructor C(C), called the copy deduction candidate.
- For each deduction-guide, a function or function template with the following properties:
In addition, if C is defined and its definition satisfies the conditions for an aggregate class ([dcl.init.aggr]) with the assumption that any dependent base class has no virtual functions and no virtual base classes, and the initializer is a non-empty braced-init-list or parenthesized expression-list, and there are no deduction-guides for C, the set contains an additional function template, called the aggregate deduction candidate, defined as follows.
For each , let be the corresponding aggregate element of C or of one of its (possibly recursive) subaggregates that would be initialized by ([dcl.init.aggr]) if
- brace elision is not considered for any aggregate element that has
- a dependent non-array type,
- an array type with a value-dependent bound, or
- an array type with a dependent array element type and is a string literal; and
- each non-trailing aggregate element that is a pack expansion is assumed to correspond to no elements of the initializer list, and
- a trailing aggregate element that is a pack expansion is assumed to correspond to all remaining elements of the initializer list (if any).
If there is no such aggregate element for any , the aggregate deduction candidate is not added to the set.
The aggregate deduction candidate is derived as above from a hypothetical constructor , where
- if is of array type and is a braced-init-list, is an rvalue reference to the declared type of , and
- if is of array type and is a string-literal, is an lvalue reference to the const-qualified declared type of , and
- otherwise, is the declared type of ,
except that additional parameter packs of the form are inserted into the parameter list in their original aggregate element position corresponding to each non-trailing aggregate element of type that was skipped because it was a parameter pack, and the trailing sequence of parameters corresponding to a trailing aggregate element that is a pack expansion (if any) is replaced by a single parameter of the form .
If A is a deducible template ([dcl.type.simple]), the set contains the guides of Awith the return type R of each guide replaced with typename CC<R>::type given a class templatetemplate <typename> class CC;whose primary template is not defined and with a single partial specialization whose template parameter list is that of A and whose template argument list is a specialization of A with the template argument list of A ([temp.dep.type]) having a member typedef type designating a template specialization with the template argument list of A but with C as the template.
[Note 2:
Equivalently, the template parameter list of the specialization is that of C, the template argument list of the specialization is B, and the member typedef names C with the template argument list of C.
— _end note_]
[Example 1: template <typename T> struct B { B(T);};template <typename T> struct C : public B<T> { using B<T>::B;};template <typename T> struct D : public B<T> {}; C c(42); D d(42); B(int) -> B<char>; C c2(42); template <typename T> struct E : public B<int> { using B<int>::B;}; E e(42); template <typename T, typename U, typename V> struct F { F(T, U, V);};template <typename T, typename U> struct G : F<U, T, int> { using G::F::F;}G g(true, 'a', 1); template<class T, std::size_t N> struct H { T array[N];};template<class T, std::size_t N> struct I { volatile T array[N];};template<std::size_t N> struct J { unsigned char array[N];}; H h = { "abc" }; I i = { "def" }; J j = { "ghi" }; — _end example_]
The guides of A are the set of functions or function templates formed as follows.
For each function or function template f in the guides of the template named by the simple-template-idof the defining-type-id, the template arguments of the return type of fare deduced from the defining-type-id of Aaccording to the process in [temp.deduct.type]with the exception that deduction does not fail if not all template arguments are deduced.
If deduction fails for another reason, proceed with an empty set of deduced template arguments.
Let g denote the result of substituting these deductions into f.
If substitution succeeds, form a function or function template f'with the following properties and add it to the set of guides of A:
- The function type of f' is the function type of g.
- If f is a function template,f' is a function template whose template parameter list consists of all the template parameters of A(including their default template arguments) that appear in the above deductions or (recursively) in their default template arguments, followed by the template parameters of f that were not deduced (including their default template arguments), otherwise f' is not a function template.
- The associated constraints ([temp.constr.decl]) are the conjunction of the associated constraints of g and a constraint that is satisfied if and only if the arguments of A are deducible (see below) from the return type.
- If f is a copy deduction candidate, then f' is considered to be so as well.
The arguments of a template A are said to be deducible from a type T if, given a class templatetemplate <typename> class AA;with a single partial specialization whose template parameter list is that of A and whose template argument list is a specialization of Awith the template argument list of A ([temp.dep.type]),AA<T> matches the partial specialization.
Initialization and overload resolution are performed as described in [dcl.init] and [over.match.ctor], [over.match.copy], or [over.match.list] (as appropriate for the type of initialization performed) for an object of a hypothetical class type, where the guides of the template named by the placeholder are considered to be the constructors of that class type for the purpose of forming an overload set, and the initializer is provided by the context in which class template argument deduction was performed.
The following exceptions apply:
- The first phase in [over.match.list](considering initializer-list constructors) is omitted if the initializer list consists of a single expression of type cv U, where U is, or is derived from, a specialization of the class template directly or indirectly named by the placeholder.
- During template argument deduction for the aggregate deduction candidate, the number of elements in a trailing parameter pack is only deduced from the number of remaining function arguments if it is not otherwise deduced.
All such notional constructors are considered to be public members of the hypothetical class type.
[Example 2: template <class T> struct A { explicit A(const T&, ...) noexcept; A(T&&, ...); };int i; A a1 = { i, i }; A a2{i, i}; A a3{0, i}; A a4 = {0, i}; template <class T> A(const T&, const T&) -> A<T&>; template <class T> explicit A(T&&, T&&) -> A<T>; A a5 = {0, 1}; A a6{0,1}; A a7 = {0, i}; A a8{0,i}; template <class T> struct B { template <class U> using TA = T;template <class U> B(U, TA<U>);}; B b{(int*)0, (char*)0}; template <typename T> struct S { T x; T y;};template <typename T> struct C { S<T> s; T t;};template <typename T> struct D { S<int> s; T t;}; C c1 = {1, 2}; C c2 = {1, 2, 3}; C c3 = {{1u, 2u}, 3}; D d1 = {1, 2}; D d2 = {1, 2, 3}; template <typename T> struct E { T t;decltype(t) t2;}; E e1 = {1, 2}; template <typename... T> struct Types {};template <typename... T> struct F : Types<T...>, T... {};struct X {};struct Y {};struct Z {};struct W { operator Y(); }; F f1 = {Types<X, Y, Z>{}, {}, {}}; F f2 = {Types<X, Y, Z>{}, X{}, Y{}}; F f3 = {Types<X, Y, Z>{}, X{}, W{}}; — _end example_]
[Example 3: template <class T, class U> struct C { C(T, U); };template<class T, class U> C(T, U) -> C<T, std::type_identity_t<U>>; template<class V> using A = C<V *, V *>;template<std::integral W> using B = A<W>;int i{};double d{}; A a1(&i, &i); A a2(i, i); A a3(&i, &d); B b1(&i, &i); B b2(&d, &d);
Possible exposition-only implementation of the above procedure: template <class> class AA;template <class V> class AA<A<V>> { };template <class T> concept deduces_A = requires { sizeof(AA<T>); };template<class T, class U> auto f1(T, U) -> C<T, U>;template<class V> requires deduces_A<C<V *, V *>> auto f1_prime(V *, V*) -> C<V *, V *>;template<class T, class U> auto f2(T, U) -> C<T, std::type_identity_t<U>>;template<class V, class U> requires deduces_A<C<V *, std::type_identity_t<U>>> auto f2_prime(V *, U) -> C<V *, std::type_identity_t<U>>;template <class> class BB;template <class V> class BB<B<V>> { };template <class T> concept deduces_B = requires { sizeof(BB<T>); };template<std::integral W> requires deduces_A<C<W *, W *>> && deduces_B<C<W *, W *>> auto f1_prime_for_B(W *, W *) -> C<W *, W *>;template<std::integral W, class U> requires deduces_A<C<W *, std::type_identity_t<U>>> && deduces_B<C<W *, std::type_identity_t<U>>> auto f2_prime_for_B(W *, U) -> C<W *, std::type_identity_t<U>>;
— _end example_]
12.2.3 Viable functions [over.match.viable]
From the set of candidate functions constructed for a given context ([over.match.funcs]), a set of viable functions is chosen, from which the best function will be selected by comparing argument conversion sequences and associated constraints ([temp.constr.decl]) for the best fit ([over.match.best]).
The selection of viable functions considers associated constraints, if any, and relationships between arguments and function parameters other than the ranking of conversion sequences.
First, to be a viable function, a candidate function shall have enough parameters to agree in number with the arguments in the list.
- If there are m arguments in the list, all candidate functions having exactly m parameters are viable.
- A candidate function having fewer than m parameters is viable only if it has an ellipsis in its parameter list ([dcl.fct]).
For the purposes of overload resolution, any argument for which there is no corresponding parameter is considered to “match the ellipsis” ([over.ics.ellipsis]). - A candidate function having more than m parameters is viable only if all parameters following the have default arguments ([dcl.fct.default]).
For the purposes of overload resolution, the parameter list is truncated on the right, so that there are exactly m parameters.
Third, forFto be a viable function, there shall exist for each argument animplicit conversion sequence that converts that argument to the corresponding parameter ofF.
If the parameter has reference type, the implicit conversion sequence includes the operation of binding the reference, and the fact that an lvalue reference to non-const cannot bind to an rvalue and that an rvalue reference cannot bind to an lvalue can affect the viability of the function (see [over.ics.ref]).
12.2.4 Best viable function [over.match.best]
12.2.4.1 General [over.match.best.general]
Define as the implicit conversion sequence that converts the argument in the list to the type of the parameter of viable function F.
[over.best.ics] defines the implicit conversion sequences and [over.ics.rank]defines what it means for one implicit conversion sequence to be a better conversion sequence or worse conversion sequence than another.
Given these definitions, a viable function is defined to be abetterfunction than another viable function if for all arguments i, is not a worse conversion sequence than , and then
- for some argument j, is a better conversion sequence than, or, if not that,
- the context is an initialization by user-defined conversion (see [dcl.init],[over.match.conv], and [over.match.ref]) and the standard conversion sequence from the result of to the destination type (i.e., the type of the entity being initialized) is a better conversion sequence than the standard conversion sequence from the result of to the destination type
[Example 1: struct A { A();operator int();operator double();} a;int i = a; float x = a; — _end example_]
or, if not that, - the context is an initialization by conversion function for direct reference binding of a reference to function type, the return type of is the same kind of reference (lvalue or rvalue) as the reference being initialized, and the return type of is not
[Example 2: template <class T> struct A { operator T&(); operator T&&(); };typedef int Fn(); A<Fn> a; Fn& lf = a; Fn&& rf = a; — _end example_]
or, if not that, - is not a function template specialization andis a function template specialization, or, if not that,
- andare function template specializations, and the function template foris more specialized than the template foraccording to the partial ordering rules described in [temp.func.order], or, if not that,
- and are non-template functions and is more partial-ordering-constrained than ([temp.constr.order])
[Example 3: template <typename T = int> struct S { constexpr void f(); constexpr void f(this S&) requires true; };void test() { S<> s; s.f(); } — _end example_]
or, if not that, - is a constructor for a class D, is a constructor for a base class B of D, and for all arguments the corresponding parameters of and have the same type
[Example 4: struct A { A(int = 0);};struct B: A { using A::A; B();};int main() { B b; } — _end example_]
or, if not that, - is a rewritten candidate ([over.match.oper]) and is not
[Example 5: struct S { friend auto operator<=>(const S&, const S&) = default; friend bool operator<(const S&, const S&); };bool b = S() < S(); — _end example_]
or, if not that, - and are rewritten candidates, and is a synthesized candidate with reversed order of parameters and is not
[Example 6: struct S { friend std::weak_ordering operator<=>(const S&, int); friend std::weak_ordering operator<=>(int, const S&); };bool b = 1 < S(); — _end example_]
or, if not that, - and are generated from class template argument deduction ([over.match.class.deduct]) for a class D, and is generated from inheriting constructors from a base class of Dwhile is not, and for each explicit function argument, the corresponding parameters of and are either both ellipses or have the same type, or, if not that,
- is generated from adeduction-guide ([over.match.class.deduct]) and is not, or, if not that,
- is the copy deduction candidateand is not, or, if not that,
- is generated from a non-template constructor and is generated from a constructor template.
[Example 7: template <class T> struct A { using value_type = T; A(value_type); A(const A&); A(T, T, int); template<class U> A(int, T, U); }; A x(1, 2, 3); template <class T>A(T) -> A<T>; A a(42); A b = a; template <class T>A(A<T>) -> A<A<T>>; A b2 = a; — _end example_]
If there is exactly one viable function that is a better function than all other viable functions, then it is the one selected by overload resolution; otherwise the call is ill-formed.105
[Example 8: void Fcn(const int*, short);void Fcn(int*, int);int i;short s = 0;void f() { Fcn(&i, s); Fcn(&i, 1L); Fcn(&i, 'c'); } — _end example_]
If the best viable function resolves to a function for which multiple declarations were found, and if any two of these declarations inhabit different scopes and specify a default argument that made the function viable, the program is ill-formed.
[Example 9: namespace A { extern "C" void f(int = 5);} namespace B { extern "C" void f(int = 5);} using A::f;using B::f;void use() { f(3); f(); } — _end example_]
12.2.4.2 Implicit conversion sequences [over.best.ics]
12.2.4.2.1 General [over.best.ics.general]
An implicit conversion sequenceis a sequence of conversions used to convert an argument in a function call to the type of the corresponding parameter of the function being called.
The sequence of conversions is an implicit conversion as defined in[conv], which means it is governed by the rules for initialization of an object or reference by a single expression ([dcl.init], [dcl.init.ref]).
Implicit conversion sequences are concerned only with the type, cv-qualification, and value category of the argument and how these are converted to match the corresponding properties of the parameter.
[Note 1:
Other properties, such as the lifetime, storage duration, linkage, alignment, accessibility of the argument, whether the argument is a bit-field, and whether a function is deleted, are ignored.
So, although an implicit conversion sequence can be defined for a given argument-parameter pair, the conversion from the argument to the parameter might still be ill-formed in the final analysis.
— _end note_]
A well-formed implicit conversion sequence is one of the following forms:
- a standard conversion sequence,
- a user-defined conversion sequence, or
- an ellipsis conversion sequence.
However, if the target is
- the first parameter of a constructor or
- the object parameter of a user-defined conversion function
and the constructor or user-defined conversion function is a candidate by
- [over.match.ctor], when the argument is the temporary in the second step of a class copy-initialization,
- [over.match.copy], [over.match.conv], or [over.match.ref](in all cases), or
- the second phase of [over.match.list]when the initializer list has exactly one element that is itself an initializer list, and the target is the first parameter of a constructor of class X, and the conversion is to X or reference to cv X,
user-defined conversion sequences are not considered.
[Note 2:
These rules prevent more than one user-defined conversion from being applied during overload resolution, thereby avoiding infinite recursion.
— _end note_]
[Example 1: struct Y { Y(int); };struct A { operator int(); }; Y y1 = A(); struct X { X(); };struct B { operator X(); }; B b; X x{{b}}; — _end example_]
For the case where the parameter type is a reference, see [over.ics.ref].
When the parameter type is not a reference, the implicit conversion sequence models a copy-initialization of the parameter from the argument expression.
The implicit conversion sequence is the one required to convert the argument expression to a prvalue of the type of the parameter.
[Note 3:
When the parameter has a class type, this is a conceptual conversion defined for the purposes of [over]; the actual initialization is defined in terms of constructors and is not a conversion.
— _end note_]
Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion.
[Example 2:
A parameter of typeAcan be initialized from an argument of typeconst A.
The implicit conversion sequence for that case is the identity sequence; it contains no “conversion” fromconst AtoA.
— _end example_]
When the parameter has a class type and the argument expression has the same type, the implicit conversion sequence is an identity conversion.
When the parameter has a class type and the argument expression has a derived class type, the implicit conversion sequence is a derived-to-baseconversion from the derived class to the base class.
A derived-to-base conversion has Conversion rank ([over.ics.scs]).
[Note 4:
There is no such standard conversion; this derived-to-base conversion exists only in the description of implicit conversion sequences.
— _end note_]
When the parameter is the implicit object parameter of a static member function, the implicit conversion sequence is a standard conversion sequence that is neither better nor worse than any other standard conversion sequence.
In all contexts, when converting to the implicit object parameter or when converting to the left operand of an assignment operation only standard conversion sequences are allowed.
[Note 5:
When a conversion to the explicit object parameter occurs, it can include user-defined conversion sequences.
— _end note_]
If no conversions are required to match an argument to a parameter type, the implicit conversion sequence is the standard conversion sequence consisting of the identity conversion ([over.ics.scs]).
If no sequence of conversions can be found to convert an argument to a parameter type, an implicit conversion sequence cannot be formed.
If there are multiple well-formed implicit conversion sequences converting the argument to the parameter type, the implicit conversion sequence associated with the parameter is defined to be the unique conversion sequence designated theambiguous conversion sequence.
For the purpose of ranking implicit conversion sequences as described in [over.ics.rank], the ambiguous conversion sequence is treated as a user-defined conversion sequence that is indistinguishable from any other user-defined conversion sequence.
[Note 6:
This rule prevents a function from becoming non-viable because of an ambiguous conversion sequence for one of its parameters.
[Example 3: class B;class A { A (B&);};class B { operator A (); };class C { C (B&); };void f(A) { } void f(C) { }B b; f(b); void f(B) { }f(b); — _end example_]
— _end note_]
If a function that uses the ambiguous conversion sequence is selected as the best viable function, the call will be ill-formed because the conversion of one of the arguments in the call is ambiguous.
The three forms of implicit conversion sequences mentioned above are defined in the following subclauses.
12.2.4.2.2 Standard conversion sequences [over.ics.scs]
Table 19summarizes the conversions defined in [conv] and partitions them into four disjoint categories: Lvalue Transformation, Qualification Adjustment, Promotion, and Conversion.
[Note 1:
These categories are orthogonal with respect to value category, cv-qualification, and data representation: the Lvalue Transformations do not change the cv-qualification or data representation of the type; the Qualification Adjustments do not change the value category or data representation of the type; and the Promotions and Conversions do not change the value category or cv-qualification of the type.
— _end note_]
[Note 2:
As described in [conv], a standard conversion sequence either is the Identity conversion by itself (that is, no conversion) or consists of one to three conversions from the other four categories.
If there are two or more conversions in the sequence, the conversions are applied in the canonical order:Lvalue Transformation,PromotionorConversion,Qualification Adjustment.
— _end note_]
Each conversion in Table 19also has an associated rank (Exact Match, Promotion, or Conversion).
These are used to rank standard conversion sequences.
The rank of a conversion sequence is determined by considering the rank of each conversion in the sequence and the rank of any reference binding.
If any of those has Conversion rank, the sequence has Conversion rank; otherwise, if any of those has Promotion rank, the sequence has Promotion rank; otherwise, the sequence has Exact Match rank.
12.2.4.2.3 User-defined conversion sequences [over.ics.user]
A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion ([class.conv]) followed by a second standard conversion sequence.
If the user-defined conversion is specified by a constructor ([class.conv.ctor]), the initial standard conversion sequence converts the source type to the type of the first parameter of that constructor.
If the user-defined conversion is specified by a conversion function, the initial standard conversion sequence converts the source type to the type of the object parameter of that conversion function.
The second standard conversion sequence converts the result of the user-defined conversion to the target type for the sequence; any reference binding is included in the second standard conversion sequence.
Since an implicit conversion sequence is an initialization, the special rules for initialization by user-defined conversion apply when selecting the best user-defined conversion for a user-defined conversion sequence (see [over.match.best] and [over.best.ics]).
If the user-defined conversion is specified by a specialization of a conversion function template, the second standard conversion sequence shall have Exact Match rank.
A conversion of an expression of class type to the same class type is given Exact Match rank, and a conversion of an expression of class type to a base class of that type is given Conversion rank, in spite of the fact that a constructor (i.e., a user-defined conversion function) is called for those cases.
12.2.4.2.4 Ellipsis conversion sequences [over.ics.ellipsis]
An ellipsis conversion sequence occurs when an argument in a function call is matched with the ellipsis parameter specification of the function called (see [expr.call]).
12.2.4.2.5 Reference binding [over.ics.ref]
When a parameter of type “reference to cv T” binds directly ([dcl.init.ref]) to an argument expression:
- If the argument expression has a type that is a derived class of the parameter type, the implicit conversion sequence is a derived-to-base conversion ([over.best.ics]).
- Otherwise, if the type of the argument is possibly cv-qualified T, or if T is an array type of unknown bound with element type U and the argument has an array type of known bound whose element type is possibly cv-qualified U, the implicit conversion sequence is the identity conversion.
- Otherwise, if T is a function type, the implicit conversion sequence is a function pointer conversion.
- Otherwise, the implicit conversion sequence is a qualification conversion.
[Example 1: struct A {};struct B : public A {} b;int f(A&);int f(B&);int i = f(b); void g() noexcept;int h(void (&)() noexcept); int h(void (&)()); int j = h(g); — _end example_]
If the parameter binds directly to the result of applying a conversion function to the argument expression, the implicit conversion sequence is a user-defined conversion sequence ([over.ics.user]) whose second standard conversion sequence is determined by the above rules.
When a parameter of reference type is not bound directly to an argument expression, the conversion sequence is the one required to convert the argument expression to the referenced type according to [over.best.ics].
Conceptually, this conversion sequence corresponds to copy-initializing a temporary of the referenced type with the argument expression.
Any difference in top-level cv-qualification is subsumed by the initialization itself and does not constitute a conversion.
Except for an implicit object parameter, for which see [over.match.funcs], an implicit conversion sequence cannot be formed if it requires binding an lvalue reference other than a reference to a non-volatile const type to an rvalue or binding an rvalue reference to an lvalue of object type.
[Note 1:
This means, for example, that a candidate function cannot be a viable function if it has a non-const lvalue reference parameter (other than the implicit object parameter) and the corresponding argument would require a temporary to be created to initialize the lvalue reference (see [dcl.init.ref]).
— _end note_]
Other restrictions on binding a reference to a particular argument that are not based on the types of the reference and the argument do not affect the formation of an implicit conversion sequence, however.
[Example 2:
A function with an “lvalue reference to int” parameter can be a viable candidate even if the corresponding argument is anintbit-field.
The formation of implicit conversion sequences treats theintbit-field as anintlvalue and finds an exact match with the parameter.
If the function is selected by overload resolution, the call will nonetheless be ill-formed because of the prohibition on binding a non-const lvalue reference to a bit-field ([dcl.init.ref]).
— _end example_]
12.2.4.2.6 List-initialization sequence [over.ics.list]
When an argument is an initializer list ([dcl.init.list]), it is not an expression and special rules apply for converting it to a parameter type.
If the initializer list is a designated-initializer-listand the parameter is not a reference, a conversion is only possible if the parameter has an aggregate type that can be initialized from the initializer list according to the rules for aggregate initialization ([dcl.init.aggr]), in which case the implicit conversion sequence is a user-defined conversion sequence whose second standard conversion sequence is an identity conversion.
[Note 1:
Aggregate initialization does not require that the members are declared in designation order.
If, after overload resolution, the order does not match for the selected overload, the initialization of the parameter will be ill-formed ([dcl.init.list]).
[Example 1: struct A { int x, y; };struct B { int y, x; };void f(A a, int); void f(B b, ...); void g(A a); void g(B b); void h() { f({.x = 1, .y = 2}, 0); f({.y = 2, .x = 1}, 0); g({.x = 1, .y = 2}); } — _end example_]
— _end note_]
Otherwise, if the parameter type is an aggregate class X and the initializer list has a single element of type cv U, where U is Xor a class derived from X, the implicit conversion sequence is the one required to convert the element to the parameter type.
Otherwise, if the parameter type is a character array106and the initializer list has a single element that is an appropriately-typedstring-literal ([dcl.init.string]), the implicit conversion sequence is the identity conversion.
Otherwise, if the parameter type is std::initializer_list<X>and all the elements of the initializer list can be implicitly converted to X, the implicit conversion sequence is the worst conversion necessary to convert an element of the list to X, or if the initializer list has no elements, the identity conversion.
This conversion can be a user-defined conversion even in the context of a call to an initializer-list constructor.
[Example 2: void f(std::initializer_list<int>); f( {} ); f( {1,2,3} ); f( {'a','b'} ); f( {1.0} ); struct A { A(std::initializer_list<double>); A(std::initializer_list<std::complex<double>>); A(std::initializer_liststd::string\); }; A a{ 1.0,2.0 }; void g(A); g({ "foo", "bar" }); typedef int IA[3];void h(const IA&); h({ 1, 2, 3 }); — _end example_]
Otherwise, if the parameter type is “array of N X” or “array of unknown bound of X”, if there exists an implicit conversion sequence from each element of the initializer list (and from {} in the former case if N exceeds the number of elements in the initializer list) to X, the implicit conversion sequence is the worst such implicit conversion sequence.
Otherwise, if the parameter is a non-aggregate class X and overload resolution per [over.match.list] chooses a single best constructor C ofX to perform the initialization of an object of type X from the argument initializer list:
- If C is not an initializer-list constructor and the initializer list has a single element of type cv U, where U is X or a class derived from X, the implicit conversion sequence has Exact Match rank if U is X, or Conversion rank if U is derived from X.
- Otherwise, the implicit conversion sequence is a user-defined conversion sequence whose second standard conversion sequence is an identity conversion.
If multiple constructors are viable but none is better than the others, the implicit conversion sequence is the ambiguous conversion sequence.
User-defined conversions are allowed for conversion of the initializer list elements to the constructor parameter types except as noted in [over.best.ics].
[Example 3: struct A { A(std::initializer_list<int>);};void f(A); f( {'a', 'b'} ); struct B { B(int, double);};void g(B); g( {'a', 'b'} ); g( {1.0, 1.0} ); void f(B); f( {'a', 'b'} ); struct C { C(std::string);};void h(C); h({"foo"}); struct D { D(A, C);};void i(D); i({ {1,2}, {"bar"} }); — _end example_]
Otherwise, if the parameter has an aggregate type which can be initialized from the initializer list according to the rules for aggregate initialization ([dcl.init.aggr]), the implicit conversion sequence is a user-defined conversion sequence whose second standard conversion sequence is an identity conversion.
[Example 4: struct A { int m1;double m2;};void f(A); f( {'a', 'b'} ); f( {1.0} ); — _end example_]
Otherwise, if the parameter is a reference, see [over.ics.ref].
[Note 2:
The rules in this subclause will apply for initializing the underlying temporary for the reference.
— _end note_]
[Example 5: struct A { int m1;double m2;};void f(const A&); f( {'a', 'b'} ); f( {1.0} ); void g(const double &); g({1}); — _end example_]
Otherwise, if the parameter type is not a class:
- if the initializer list has one element that is not itself an initializer list, the implicit conversion sequence is the one required to convert the element to the parameter type;
[Example 6: void f(int); f( {'a'} ); f( {1.0} ); — _end example_] - if the initializer list has no elements, the implicit conversion sequence is the identity conversion.
[Example 7: void f(int); f( { } ); — _end example_]
In all cases other than those enumerated above, no conversion is possible.
12.2.4.3 Ranking implicit conversion sequences [over.ics.rank]
This subclause defines a partial ordering of implicit conversion sequences based on the relationshipsbetter conversion sequenceandbetter conversion.
If an implicit conversion sequence S1 is defined by these rules to be a better conversion sequence than S2, then it is also the case that S2 is aworse conversion sequencethan S1.
If conversion sequence S1 is neither better than nor worse than conversion sequence S2, S1 and S2 are said to beindistinguishable conversion sequences.
When comparing the basic forms of implicit conversion sequences (as defined in [over.best.ics])
- a standard conversion sequence is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and
- a user-defined conversion sequence is a better conversion sequence than an ellipsis conversion sequence.
Two implicit conversion sequences of the same form are indistinguishable conversion sequences unless one of the following rules applies:
- List-initialization sequence L1 is a better conversion sequence than list-initialization sequence L2 if
- L1 converts to std::initializer_list<X> for some X andL2 does not, or, if not that,
- L1 and L2 convert to arrays of the same element type, and either the number of elements initialized by L1is less than the number of elements initialized by L2, or andL2 converts to an array of unknown bound and L1 does not,
even if one of the other rules in this paragraph would otherwise apply.
[Example 1: void f1(int); void f1(std::initializer_list<long>); void g1() { f1({42}); } void f2(std::pair<const char*, const char*>); void f2(std::initializer_liststd::string\); void g2() { f2({"foo","bar"}); } — end example_]
[_Example 2: void f(int (&&)[] ); void f(double (&&)[] ); void f(int (&&)[2]); f( {1} ); f( {1.0} ); f( {1.0, 2.0} ); f( {1, 2} ); — _end example_]
- Standard conversion sequenceS1is a better conversion sequence than standard conversion sequenceS2if
- S1is a proper subsequence ofS2(comparing the conversion sequences in the canonical form defined by [over.ics.scs], excluding any Lvalue Transformation; the identity conversion sequence is considered to be a subsequence of any non-identity conversion sequence) or, if not that,
- the rank ofS1is better than the rank ofS2, orS1andS2have the same rank and are distinguishable by the rules in the paragraph below, or, if not that,
- S1 and S2 include reference bindings ([dcl.init.ref]) and neither refers to an implicit object parameter of a non-static member function declared without a ref-qualifier, and S1 binds an rvalue reference to an rvalue and S2 binds an lvalue reference
[Example 3: int i;int f1();int&& f2();int g(const int&);int g(const int&&);int j = g(i); int k = g(f1()); int l = g(f2()); struct A { A& operator<<(int);void p() &;void p() &&;}; A& operator<<(A&&, char); A() << 1; A() << 'c'; A a; a << 1; a << 'c'; A().p(); a.p(); — _end example_]
or, if not that, - S1 and S2 include reference bindings ([dcl.init.ref]) andS1 binds an lvalue reference to an lvalue of function type andS2 binds an rvalue reference to an lvalue of function type
[Example 4: int f(void(&)()); int f(void(&&)()); void g();int i1 = f(g); — _end example_]
or, if not that, - S1 and S2 differ only in their qualification conversion ([conv.qual]) and yield similar types T1 and T2, respectively (where a standard conversion sequence that is a reference binding is considered to yield the cv-unqualified referenced type), where T1 and T2 are not the same type, andconst T2 is reference-compatible with T1 ([dcl.init.ref])
[Example 5: int f(const volatile int *);int f(const int *);int i;int j = f(&i); int g(const int*);int g(const volatile int* const&);int* p;int k = g(p); — _end example_]
or, if not that, - S1andS2bind “reference to T1” and “reference to T2”, respectively ([dcl.init.ref]), where T1 and T2 are not the same type, andT2 is reference-compatible with T1
[Example 6: int f(const int &);int f(int &);int g(const int &);int g(int);int i;int j = f(i); int k = g(i); struct X { void f() const;void f();};void g(const X& a, X b) { a.f(); b.f(); } int h(int (&)[]);int h(int (&)[1]);void g2() { int a[1]; h(a); } — _end example_]
or, if not that, - S1 and S2bind the same reference type “reference to T” and have source types V1 and V2, respectively, where the standard conversion sequence from V1* to T*is better than the standard conversion sequence from V2* to T*.
[Example 7: struct Z {};struct A { operator Z&();operator const Z&(); };struct B { operator Z();operator const Z&&(); };const Z& r1 = A(); const Z&& r2 = B(); — _end example_]
- User-defined conversion sequenceU1is a better conversion sequence than another user-defined conversion sequenceU2if they contain the same user-defined conversion function or constructor or they initialize the same class in an aggregate initialization and in either case the second standard conversion sequence ofU1is better than the second standard conversion sequence ofU2.
[Example 8: struct A { operator short();} a;int f(int);int f(float);int i = f(a); — _end example_]
Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion.
Two conversion sequences with the same rank are indistinguishable unless one of the following rules applies:
- A conversion that does not convert a pointer or a pointer to member toboolis better than one that does.
- A conversion that promotes an enumeration whose underlying type is fixed to its underlying type is better than one that promotes to the promoted underlying type, if the two are different.
- A conversion in either direction between floating-point type FP1 and floating-point type FP2is better than a conversion in the same direction between FP1 and arithmetic type T3 if
- the floating-point conversion rank ([conv.rank]) of FP1is equal to the rank of FP2, and
- T3 is not a floating-point type, orT3 is a floating-point type whose rank is not equal to the rank of FP1, or the floating-point conversion subrank ([conv.rank]) of FP2is greater than the subrank of T3.
[Example 9: int f(std::float32_t);int f(std::float64_t);int f(long long);float x; std::float16_t y;int i = f(x); int j = f(y); — _end example_]
- If classBis derived directly or indirectly from classA, conversion ofB*toA*is better than conversion ofB*tovoid*, and conversion ofA*tovoid*is better than conversion ofB*tovoid*.
- If classBis derived directly or indirectly from classAand classCis derived directly or indirectly fromB,
- conversion ofC*toB*is better than conversion ofC*toA*,
[Example 10: struct A {};struct B : public A {};struct C : public B {}; C* pc;int f(A*);int f(B*);int i = f(pc); — _end example_] - binding of an expression of typeCto a reference to typeBis better than binding an expression of typeCto a reference to typeA,
- conversion ofA::*toB::*is better than conversion ofA::*toC::*,
- conversion ofCtoBis better than conversion ofCtoA,
- conversion ofB*toA*is better than conversion ofC*toA*,
- binding of an expression of typeBto a reference to typeAis better than binding an expression of typeCto a reference to typeA,
- conversion ofB::*toC::*is better than conversion ofA::*toC::*, and
- conversion ofBtoAis better than conversion ofCtoA.
[Note 1:
Compared conversion sequences will have different source types only in the context of comparing the second standard conversion sequence of an initialization by user-defined conversion (see [over.match.best]); in all other contexts, the source types will be the same and the target types will be different.
— _end note_]
- conversion ofC*toB*is better than conversion ofC*toA*,
12.3 Address of an overload set [over.over]
An id-expressionwhose terminal name refers to an overload set S and that appears without arguments is resolved to a function, a pointer to function, or a pointer to member function for a specific function that is chosen from a set of functions selected from Sdetermined based on the target type required in the context (if any), as described below.
The target can be
- an object or reference being initialized ([dcl.init], [dcl.init.ref], [dcl.init.list]),
- the left side of an assignment ([expr.assign]),
- a parameter of a function ([expr.call]),
- a parameter of a user-defined operator,
- the return value of a function, operator function, or conversion ([stmt.return]),
- an explicit type conversion ([expr.type.conv], [expr.static.cast], [expr.cast]), or
- a constant template parameter ([temp.arg.nontype]).
If the target type contains a placeholder type, placeholder type deduction is performed ([dcl.type.auto.deduct]), and the remainder of this subclause uses the target type so deduced.
[Note 1:
Any redundant set of parentheses surrounding the function name is ignored ([expr.prim.paren]).
— _end note_]
If there is no target, all non-template functions named are selected.
Otherwise, a non-template function with type Fis selected for the function type FT of the target type if F(after possibly applying the function pointer conversion ([conv.fctptr])) is identical to FT.
[Note 2:
That is, the class of which the function is a member is ignored when matching a pointer-to-member-function type.
— _end note_]
Non-member functions, static member functions, and explicit object member functions match targets of function pointer type or reference to function type.
Implicit object member functions match targets of pointer-to-member-function type.
[Note 3:
If an implicit object member function is chosen, the result can be used only to form a pointer to member ([expr.unary.op]).
— _end note_]
All functions with associated constraints that are not satisfied ([temp.constr.decl]) are eliminated from the set of selected functions.
If more than one function in the set remains, all function template specializations in the set are eliminated if the set also contains a function that is not a function template specialization.
Any given non-template functionF0is eliminated if the set contains a second non-template function that is more partial-ordering-constrained thanF0 ([temp.constr.order]).
Any given function template specializationF1is eliminated if the set contains a second function template specialization whose function template is more specialized than the function template ofF1according to the partial ordering rules of [temp.func.order].
After such eliminations, if any, there shall remain exactly one selected function.
[Example 1: int f(double);int f(int);int (*pfd)(double) = &f; int (*pfi)(int) = &f; int (*pfe)(...) = &f; int (&rfi)(int) = f; int (&rfd)(double) = f; void g() { (int (*)(int))&f; }
The initialization ofpfeis ill-formed because nof()with typeint(...)has been declared, and not because of any ambiguity.
— _end example_]
[Example 2: struct X { int f(int);static int f(long);};int (X::*p1)(int) = &X::f; int (*p2)(int) = &X::f; int (*p3)(long) = &X::f; int (X::*p4)(long) = &X::f; int (X::*p5)(int) = &(X::f); int (*p6)(long) = &(X::f); — _end example_]
[Example 3: template<bool B> struct X { void f(short) requires B;void f(long);template<typename> void g(short) requires B;template<typename> void g(long);};void test() { &X<true>::f; &X<true>::g<int>; } — _end example_]
[Note 4:
If f and g are both overload sets, the Cartesian product of possibilities is considered to resolve f(&g), or the equivalent expression f(g).
— _end note_]
[Note 5:
Even if B is a public base of D, we haveD* f(); B* (*p1)() = &f; void g(D*);void (*p2)(B*) = &g;
— _end note_]
12.4 Overloaded operators [over.oper]
12.4.1 General [over.oper.general]
A declaration whose declarator-id is an operator-function-idshall declare a function or function template or an explicit instantiation or specialization of a function template.
A function so declared is an operator function.
A function template so declared is an operator function template.
A specialization of an operator function template is also an operator function.
An operator function is said toimplementthe operator named in itsoperator-function-id.
operator: one of
new delete new[] delete[] co_await ( ) [ ] -> ->*
~ ! + - * / % ^ &
| = += -= *= /= %= ^= &=
|= == != < > <= >= <=> &&
|| << >> <<= >>= ++ -- ,
[Note 1:
The operatorsnew[],delete[],(), and[]are formed from more than one token.
— _end note_]
Both the unary and binary forms of
- - * &
can be overloaded.
Operator functions are usually not called directly; instead they are invoked to evaluate the operators they implement ([over.unary] – [over.inc]).
They can be explicitly called, however, using theoperator-function-idas the name of the function in the function call syntax ([expr.call]).
[Example 1: complex z = a.operator+(b); void* p = operator new(sizeof(int)*n); — _end example_]
The allocation and deallocation functions,operator new,operator new[],operator delete, andoperator delete[], are described completely in [basic.stc.dynamic].
The attributes and restrictions found in the rest of [over.oper] do not apply to them unless explicitly stated in [basic.stc.dynamic].
The co_await operator is described completely in [expr.await].
The attributes and restrictions found in the rest of [over.oper] do not apply to it unless explicitly stated in [expr.await].
An operator function shall have at least one function parameter or implicit object parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration.
It is not possible to change the precedence, grouping, or number of operands of operators.
The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class types by defining operator functions that implement these operators.
Likewise, the meaning of the operators (unary) & and , (comma) can be changed for specific enumeration types.
Operator functions are inherited in the same manner as other base class functions.
An operator function shall be a prefix unary, binary, function call, subscripting, class member access, increment, or decrement operator function.
[Note 3:
The identities among certain predefined operators applied to fundamental types (for example,++a ≡ a+=1) need not hold for operator functions.
Some predefined operators, such as+=, require an operand to be an lvalue when applied to fundamental types; this is not required by operator functions.
— _end note_]
An operator function cannot have default arguments, except where explicitly stated below.
Operator functions cannot have more or fewer parameters than the number required for the corresponding operator, as described in the rest of [over.oper].
Operators not mentioned explicitly in subclauses [over.assign] through [over.inc]act as ordinary unary and binary operators obeying the rules of [over.unary] or [over.binary].
12.4.2 Unary operators [over.unary]
A prefix unary operator functionis a function named operator@for a prefix unary-operator @ ([expr.unary.op]) that is either a non-static member function ([class.mfct]) with no non-object parameters or a non-member function with one parameter.
If a member function is selected, the expression is interpreted as
Otherwise, if a non-member function is selected, the expression is interpreted as
[Note 2:
The unary and binary forms of the same operator have the same name.
Consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa.
— _end note_]
12.4.3 Binary operators [over.binary]
12.4.3.1 General [over.binary.general]
A binary operator functionis a function named operator@for a binary operator @ that is either a non-static member function ([class.mfct]) with one non-object parameter or a non-member function with two parameters.
For an expression x @ y with subexpressions x and y, the operator function is selected by overload resolution ([over.match.oper]).
If a member function is selected, the expression is interpreted as
x . operator @ ( y )
Otherwise, if a non-member function is selected, the expression is interpreted as
operator @ ( x , y )
An equality operator function is an operator function for an equality operator ([expr.eq]).
A relational operator function is an operator function for a relational operator ([expr.rel]).
A three-way comparison operator function is an operator function for the three-way comparison operator ([expr.spaceship]).
A comparison operator function is an equality operator function, a relational operator function, or a three-way comparison operator function.
12.4.3.2 Simple assignment [over.assign]
A simple assignment operator functionis a binary operator function named operator=.
A simple assignment operator function shall be a non-static member function.
[Note 1:
Because only standard conversion sequences are considered when converting to the left operand of an assignment operation ([over.best.ics]), an expression x = y with a subexpression x of class type is always interpreted as x.operator=(y).
— _end note_]
[Note 2:
Since a copy assignment operator is implicitly declared for a class if not declared by the user ([class.copy.assign]), a base class assignment operator function is always hidden by the copy assignment operator function of the derived class.
— _end note_]
[Note 3:
Any assignment operator function, even the copy and move assignment operators, can be virtual.
For a derived class D with a base class Bfor which a virtual copy/move assignment has been declared, the copy/move assignment operator in D does not overrideB's virtual copy/move assignment operator.
[Example 1: struct B { virtual int operator= (int);virtual B& operator= (const B&);};struct D : B { virtual int operator= (int);virtual D& operator= (const B&);}; D dobj1; D dobj2; B* bptr = &dobj1;void f() { bptr->operator=(99); *bptr = 99; bptr->operator=(dobj2); *bptr = dobj2; dobj1 = dobj2; } — _end example_]
— _end note_]
12.4.4 Function call [over.call]
A function call operator functionis a function named operator()that is a member function with an arbitrary number of parameters.
It may have default arguments.
For an expression of the form
where the postfix-expression is of class type, the operator function is selected by overload resolution ([over.call.object]).
If a surrogate call function is selected, let e be the result of invoking the corresponding conversion operator function on the postfix-expression;
the expression is interpreted as
Otherwise, the expression is interpreted as
12.4.5 Subscripting [over.sub]
A subscripting operator functionis a member function named operator[]with an arbitrary number of parameters.
It may have default arguments.
For an expression of the form
the operator function is selected by overload resolution ([over.match.oper]).
If a member function is selected, the expression is interpreted as
[Example 1: struct X { Z operator[](std::initializer_list<int>); Z operator[](auto...);}; X x; x[{1,2,3}] = 7; x[1,2,3] = 7; int a[10]; a[{1,2,3}] = 7; a[1,2,3] = 7; — _end example_]
12.4.6 Class member access [over.ref]
A class member access operator functionis a function named operator->that is a non-static member function taking no non-object parameters.
For an expression of the form
the operator function is selected by overload resolution ([over.match.oper]), and the expression is interpreted as
12.4.7 Increment and decrement [over.inc]
An increment operator functionis a function named operator++.
If this function is a non-static member function with no non-object parameters, or a non-member function with one parameter, it defines the prefix increment operator++for objects of that type.
If the function is a non-static member function with one non-object parameter (which shall be of typeint) or a non-member function with two parameters (the second of which shall be of typeint), it defines the postfix increment operator++for objects of that type.
When the postfix increment is called as a result of using the++operator, theintargument will have value zero.107
[Example 1: struct X { X& operator++(); X operator++(int); };struct Y { }; Y& operator++(Y&); Y operator++(Y&, int); void f(X a, Y b) { ++a; a++; ++b; b++; a.operator++(); a.operator++(0); operator++(b); operator++(b, 0); } — _end example_]
A decrement operator functionis a function named operator--and is handled analogously to an increment operator function.
12.5 Built-in operators [over.built]
The candidate operator functions that represent the built-in operators defined in [expr.compound] are specified in this subclause.
These candidate functions participate in the operator overload resolution process as described in [over.match.oper] and are used for no other purpose.
[Note 1:
Because built-in operators take only operands with non-class type, and operator overload resolution occurs only when an operand expression originally has class or enumeration type, operator overload resolution can resolve to a built-in operator only when an operand has a class type that has a user-defined conversion to a non-class type appropriate for the operator, or when an operand has an enumeration type that can be converted to a type appropriate for the operator.
Also note that some of the candidate operator functions given in this subclause are more permissive than the built-in operators themselves.
As described in [over.match.oper], after a built-in operator is selected by overload resolution the expression is subject to the requirements for the built-in operator given in [expr.compound], and therefore to any additional semantic constraints given there.
In some cases, user-written candidates with the same name and parameter types as a built-in candidate operator function cause the built-in operator function to not be included in the set of candidate functions.
— _end note_]
In this subclause, the termpromoted integral typeis used to refer to those cv-unqualified integral types which are preserved byintegral promotion (including e.g.intandlongbut excluding e.g.char).
[Note 2:
In all cases where a promoted integral type is required, an operand of unscoped enumeration type will be acceptable by way of the integral promotions.
— _end note_]
In the remainder of this subclause, vq represents eithervolatile or no cv-qualifier.
For every pair (T,vq), where_T_is a cv-unqualified arithmetic type other than boolor a cv-unqualified pointer to (possibly cv-qualified) object type, there exist candidate operator functions of the form_vq_ T& operator++(vq T&);T operator++(vq T&, int);vq T& operator--(vq T&);T operator--(vq T&, int);
For every (possibly cv-qualified) object type T and for every function type T_that has neither cv-qualifiers nor a ref-qualifier, there exist candidate operator functions of the form_T& operator*(T*);
For every type T there exist candidate operator functions of the form_T_* operator+(T*);
For every cv-unqualified floating-point or promoted integral type T, there exist candidate operator functions of the form_T_ operator+(T);T operator-(T);
For every promoted integral type_T_, there exist candidate operator functions of the form_T_ operator~(T);
For every quintuple (C1,C2,T,cv1,cv2), where_C2_is a class type,C1_is the same type as C2 or is a derived class of C2, and_T_is an object type or a function type, there exist candidate operator functions of the form_cv12 T& operator->*(cv1 C1*, cv2 T C2::*);where cv12 is the union of cv1 and cv2.
The return type is shown for exposition only; see [expr.mptr.oper] for the determination of the operator's result type.
For every pair of types L and R, where each of L and R is a floating-point or promoted integral type, there exist candidate operator functions of the form_LR_ operator*(L, R);LR operator/(L, R);LR operator+(L, R);LR operator-(L, R);bool operator==(L, R);bool operator!=(L, R);bool operator<(L_, R);bool operator>(L, R);bool operator<=(_L_, _R_);bool operator>=(L, R);where_LR_is the result of the usual arithmetic conversions ([expr.arith.conv]) between types_L_and_R.
For every integral type _T_there exists a candidate operator function of the formstd::strong_ordering operator<=>(T, T);
For every pair of floating-point types_L_ and R, there exists a candidate operator function of the formstd::partial_ordering operator<=>(L, R);
For every cv-qualified or cv-unqualified object type_T_there exist candidate operator functions of the form_T_* operator+(T*, std::ptrdiff_t);T& operator[](_T_*, std::ptrdiff_t);T* operator-(T*, std::ptrdiff_t);T* operator+(std::ptrdiff_t, T*);T& operator[](std::ptrdiff_t, _T_*);
For every_T_, where_T_is a pointer to object type, there exist candidate operator functions of the formstd::ptrdiff_t operator-(T, T);
For every T, where T is an enumeration type or a pointer type, there exist candidate operator functions of the formbool operator==(T, T);bool operator!=(T, T);bool operator<(_T_, _T_);bool operator>(T, T);bool operator<=(_T_, _T_);bool operator>=(T, T);R operator<=>(T, T);where R is the result type specified in [expr.spaceship].
For every T, where _T_is a pointer-to-member type or std::nullptr_t, there exist candidate operator functions of the formbool operator==(T, T);bool operator!=(T, T);
For every pair of promoted integral types_L_and_R_, there exist candidate operator functions of the form_LR_ operator%(L, R);LR operator&(L, R);LR operator^(L, R);LR operator|(L, R);L operator<<(L_, R);L operator>>(L, R);where_LR_is the result of the usual arithmetic conversions ([expr.arith.conv]) between types_L_and_R.
For every triple (L, vq, R), where L is an arithmetic type, and R is a floating-point or promoted integral type, there exist candidate operator functions of the form_vq_ L& operator=(vq L&, R);vq L& operator*=(vq L&, R);vq L& operator/=(vq L&, R);vq L& operator+=(vq L&, R);vq L& operator-=(vq L&, R);
For every pair (T, vq), where T is any type, there exist candidate operator functions of the form_T_*vq& operator=(T*vq&, T*);
For every pair (T,vq), where_T_is an enumeration or pointer-to-member type, there exist candidate operator functions of the form_vq_ T& operator=(vq T&, T);
For every pair (T,vq), where_T_is a cv-qualified or cv-unqualified object type, there exist candidate operator functions of the form_T_*vq& operator+=(T*vq&, std::ptrdiff_t);T*vq& operator-=(T*vq&, std::ptrdiff_t);
For every triple (L,vq,R), where_L_is an integral type, and_R_is a promoted integral type, there exist candidate operator functions of the form_vq_ L& operator%=(vq L&, R);vq L& operator<<=(_vq_ _L_&, _R_);_vq_ _L_& operator>>=(vq L&, R);vq L& operator&=(vq L&, R);vq L& operator^=(vq L&, R);vq L& operator|=(vq L&, R);
There also exist candidate operator functions of the formbool operator!(bool);bool operator&&(bool, bool);bool operator||(bool, bool);
For every pair of types L and R, where each of L and R is a floating-point or promoted integral type, there exist candidate operator functions of the form_LR_ operator?:(bool, L, R);where_LR_is the result of the usual arithmetic conversions ([expr.arith.conv]) between types_L_and_R_.
[Note 3:
As with all these descriptions of candidate functions, this declaration serves only to describe the built-in operator for purposes of overload resolution.
The operator “?:” cannot be overloaded.
— _end note_]
For every type_T_, where_T_is a pointer, pointer-to-member, or scoped enumeration type, there exist candidate operator functions of the form_T_ operator?:(bool, T, T);
12.6 User-defined literals [over.literal]
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.
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-idis a literal operator template.
The declaration of a literal operator shall have aparameter-declaration-clause equivalent to one of the following:const char* unsigned long long int long double char wchar_t char8_t char16_t char32_t const char*, std::size_tconst wchar_t*, std::size_tconst char8_t*, std::size_tconst char16_t*, std::size_tconst char32_t*, std::size_t
A raw literal operator is a literal operator with a single parameter whose type is const char*.
The declaration of a literal operator template shall have an empty parameter-declaration-clauseand shall declare either a numeric literal operator template or a string literal operator template.
Literal operators and literal operator templates shall not have C language linkage.
[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_]
[Example 1: void operator ""_km(long double); string operator "" _i18n(const char*, std::size_t); template <char...> double operator ""_\u03C0(); float operator ""_e(const char*); float operator ""E(const char*); double operator""_Bq(long double); double operator"" _Bq(long double); float operator " "B(const char*); string operator ""5X(const char*, std::size_t); double operator ""_miles(double); template <char...> int operator ""_j(const char*); extern "C" void operator ""_m(long double); — _end example_]