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

12 KiB
Raw Blame History

[over.built]

12 Overloading [over]

12.5 Built-in operators [over.built]

1

#

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]

2

#

In this subclause, the termpromoted integral type is used to refer to those cv-unqualified integral types which are preserved byintegral promotion (including e.g.int andlong but 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]

3

#

In the remainder of this subclause, vq represents eithervolatile or no cv-qualifier.

4

#

For every pair (T,vq), whereT is a cv-unqualified arithmetic type other than bool or a cv-unqualified pointer to (possibly cv-qualified) object type, there exist candidate operator functions of the formvq T& operator++(vq T&);T operator++(vq T&, int);vq T& operator--(vq T&);T operator--(vq T&, int);

5

#

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 formT& operator*(T*);

6

#

For every type T there exist candidate operator functions of the form*T** operator+(T*);

7

#

For every cv-unqualified floating-point or promoted integral type T, there exist candidate operator functions of the formT operator+(T);T operator-(T);

8

#

For every promoted integral typeT, there exist candidate operator functions of the formT operator~(T);

9

#

For every quintuple (C1,C2,T,cv1,cv2), whereC2 is a class type,C1 is the same type as C2 or is a derived class of C2, andT is an object type or a function type, there exist candidate operator functions of the formcv12 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.

10

#

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 formLR 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); whereLR is the result of the usual arithmetic conversions ([expr.arith.conv]) between typesL andR.

11

#

For every integral type T there exists a candidate operator function of the formstd::strong_ordering operator<=>(T, T);

12

#

For every pair of floating-point typesL and R, there exists a candidate operator function of the formstd::partial_ordering operator<=>(L, R);

13

#

For every cv-qualified or cv-unqualified object typeT 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*);

14

#

For everyT, whereT is a pointer to object type, there exist candidate operator functions of the formstd::ptrdiff_t operator-(T, T);

15

#

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].

16

#

For every T, where T is a pointer-to-member type, std::meta::info, or std::nullptr_t, there exist candidate operator functions of the formbool operator==(T, T);bool operator!=(T, T);

17

#

For every pair of promoted integral typesL andR, there exist candidate operator functions of the formLR operator%(L, R);LR operator&(L, R);LR operator^(L, R);LR operator|(L, R);L operator<<(L, R);L operator>>(L, R); whereLR is the result of the usual arithmetic conversions ([expr.arith.conv]) between typesL andR.

18

#

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 formvq 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);

19

#

For every pair (T, vq), where T is any type, there exist candidate operator functions of the formTvq& operator=(Tvq&, T*);

20

#

For every pair (T,vq), whereT is an enumeration or pointer-to-member type, there exist candidate operator functions of the formvq T& operator=(vq T&, T);

21

#

For every pair (T,vq), whereT is a cv-qualified or cv-unqualified object type, there exist candidate operator functions of the formTvq& operator+=(Tvq&, std::ptrdiff_t);Tvq& operator-=(Tvq&, std::ptrdiff_t);

22

#

For every triple (L,vq,R), whereL is an integral type, andR is a promoted integral type, there exist candidate operator functions of the formvq 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);

23

#

There also exist candidate operator functions of the formbool operator!(bool);bool operator&&(bool, bool);bool operator||(bool, bool);

24

#

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 formLR operator?:(bool, L, R); whereLR is the result of the usual arithmetic conversions ([expr.arith.conv]) between typesL andR.

[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]

25

#

For every typeT, whereT is a pointer, pointer-to-member, or scoped enumeration type, there exist candidate operator functions of the formT operator?:(bool, T, T);