Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/operators"

From cppreference.com
< cpp‎ | language
m (fmt, http -> https)
m (fmt)
Line 1: Line 1:
{{title|operator overloading}}
+
<nowiki><!--{{title|operator overloading}}
 
{{cpp/language/expressions/navbar}}
 
{{cpp/language/expressions/navbar}}
  
Line 505: Line 505:
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 +
-->

Revision as of 04:39, 9 October 2023

<!--{{title|operator overloading}} {{cpp/language/expressions/navbar}} Customizes the C++ operators for operands of user-defined types. ===Syntax=== Overloaded operators are {{rlp|functions}} with special function names: {{sdsc begin}} {{sdsc|num=1|{{ttb|operator}} {{spar|op}}}} {{sdsc|num=2|{{ttb|operator}} {{spar|type}}}} {{sdsc|num=3|{{ttb|operator}} {{ttb|new}}<br>{{ttb|operator}} {{ttb|new []}}}} {{sdsc|num=4|{{ttb|operator}} {{ttb|delete}}<br>{{ttb|operator}} {{ttb|delete []}}}} {{sdsc|num=5|notes={{mark since c++11}}|{{ttb|operator}} {{ttb|""}} {{spar|suffix-identifier}}}} {{sdsc|num=6|notes={{mark since c++20}}|{{ttb|operator}} {{ttb|co_await}}}} {{sdsc end}} {{par begin}} {{par|{{spar|op}}|any of the following operators:{{c|+}} {{c|-}} {{c|*}} {{c|/}} {{c|%}} {{c|^}} {{c|&}} {{c|{{!}}}} {{c|~}} {{c|!}} {{c|1==}} {{c|<}} {{c|>}} {{c|1=+=}} {{c|1=-=}} {{c|1=*=}} {{c|1=/=}} {{c|1=%=}} {{c|1=^=}} {{c|1=&=}} {{c|1={{!}}=}} {{c|<<}} {{c|>>}} {{c|1=>>=}} {{c|1=<<=}} {{c|1===}} {{c|1=!=}} {{c|1=<=}} {{c|1=>=}} {{rev inl|since=c++20|{{c|1=<=>}}}} {{c|&&}} {{c|{{!}}{{!}}}} {{c|++}} {{c|--}} {{c|,}} {{c|->*}} {{c|->}} {{c|( )}} {{c|[ ]}}}} {{par end}} @1@ overloaded operator; @2@ {{rlp|cast operator|user-defined conversion function}}; @3@ [[cpp/memory/new/operator new|allocation function]]; @4@ [[cpp/memory/new/operator delete|deallocation function]]; @5@ {{rlp|user literal|user-defined literal}}; @6@ overloaded {{tt|co_await}} operator for use in {{rlp|coroutines#co_await|co_await expressions}}. ===Overloaded operators=== When an operator appears in an {{rlp|expressions|expression}}, and at least one of its operands has a {{rlp|class|class type}} or an {{rlp|enum|enumeration type}}, then {{rlp|overload resolution}} is used to determine the user-defined function to be called among all the functions whose signatures match the following: {|class="wikitable" style="font-size:85%; text-align:left;" |- !Expression !As member function !As non-member function !Example |- |@a |(a).operator@ ( ) |operator@ (a) |{{c|!std::cin}} calls {{c|std::cin.operator!()}} |- |a@b |(a).operator@ (b) |operator@ (a, b) |{{c|std::cout << 42}} calls {{c|std::cout.operator<<(42)}} |- |a=b |(a).operator= (b) |{{no|cannot be non-member}} |Given {{c|std::string s;}}, {{c|1=s = "abc";}} calls {{c|1=s.operator=("abc")}} |- |a(b...) |(a).operator()(b...) |{{no|cannot be non-member}} |Given {{c|std::random_device r;}}, {{c|1=auto n = r();}} calls {{c|r.operator()()}} |- |a[b...] |(a).operator[](b...) |{{no|cannot be non-member}} |Given {{c|std::map<int, int> m;}}, {{c|1=m[1] = 2;}} calls {{c|m.operator[](1)}} |- |a-> |(a).operator-> ( ) |{{no|cannot be non-member}} |Given {{c|std::unique_ptr<S> p;}}, {{c|p->bar()}} calls {{c|p.operator->()}} |- |a@ |(a).operator@ (0) |operator@ (a, 0) |Given {{c|std::vector<int>::iterator i;}}, {{c|i++}} calls {{c|i.operator++(0)}} |- |colspan="4"| In this table, {{ttb|@}} is a placeholder representing all matching operators: all prefix operators in @a, all postfix operators other than -> in a@, all infix operators other than = in a@b. |} {{rrev|since=c++20| In addition, for comparison operators {{c|{{==}}}}, {{c|!{{=}}}}, {{c|<}}, {{c|>}}, {{c|<{{=}}}}, {{c|>{{=}}}}, {{c|<{{=}}>}}, overload resolution also considers the ''rewritten candidates'' generated from {{c|operator{{==}}}} or {{c|operator<{{=}}>}}. }} Note: for overloading {{rev inl|since=c++20|{{rlpt|coroutines#co_await|co_await}}, }}{{rlp|cast operator|user-defined conversion functions}}, {{rlp|user literal|user-defined literals}}, [[cpp/memory/new/operator new|allocation]] and [[cpp/memory/new/operator delete|deallocation]] see their respective articles. Overloaded operators (but not the built-in operators) can be called using function notation: {{source|1= std::string str = "Hello, "; str.operator+=("world"); // same as str += "world"; operator<<(operator<<(std::cout, str), '\n'); // same as std::cout << str << '\n'; // (since C++17) except for sequencing }} ===Restrictions=== * The operators {{ttb|::}} (scope resolution), {{ttb|.}} (member access), {{ttb|.*}} (member access through pointer to member), and {{ttb|?:}} (ternary conditional) cannot be overloaded. * New operators such as {{ttb|**}}, {{ttb|<>}}, or {{ttb|&{{!}}}} cannot be created. * It is not possible to change the precedence, grouping, or number of operands of operators. * The overload of operator {{ttb|->}} must either return a raw pointer, or return an object (by reference or by value) for which operator {{ttb|->}} is in turn overloaded. * The overloads of operators {{ttb|&&}} and {{ttb|{{!!}}}} lose short-circuit evaluation. {{rev begin}} {{rev|until=c++17| * {{ttb|&&}}, {{ttb|{{!!}}}}, and {{ttb|,}} (comma) lose their special {{rlp|eval order|sequencing properties}} when overloaded and behave like regular function calls even when they are used without function-call notation.}} {{rev end}} ===Canonical implementations=== Besides the restrictions above, the language puts no other constraints on what the overloaded operators do, or on the return type (it does not participate in overload resolution), but in general, overloaded operators are expected to behave as similar as possible to the built-in operators: {{c|operator+}} is expected to add, rather than multiply its arguments, {{c|operator{{=}}}} is expected to assign, etc. The related operators are expected to behave similarly ({{c|operator+}} and {{c|operator+{{=}}}} do the same addition-like operation). The return types are limited by the expressions in which the operator is expected to be used: for example, assignment operators return by reference to make it possible to write {{c|1=a = b = c = d}}, because the built-in operators allow that. Commonly overloaded operators have the following typical, canonical forms:<ref>[https://stackoverflow.com/questions/4421706/4421719#4421719 Operator Overloading] on StackOverflow C++ FAQ</ref> ====Assignment operator==== The assignment operator ({{c|1=operator=}}) has special properties: see {{rlp|copy assignment}} and {{rlp|move assignment}} for details. The canonical copy-assignment operator is expected to [https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c62-make-copy-assignment-safe-for-self-assignment be safe on self-assignment], and to return the lhs by reference: {{source|1= // copy assignment T& operator=(const T& other) { // Guard self assignment if (this == &other) return *this; // assume *this manages a reusable resource, such as a heap-allocated buffer mArray if (size != other.size) // resource in *this cannot be reused { temp = new int[other.size]; // allocate resource, if throws, do nothing delete[] mArray; // release resource in *this mArray = temp; size = other.size; } std::copy(other.mArray, other.mArray + other.size, mArray); return *this; } }} {{rrev|since=c++11| The canonical move assignment is expected to [https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c64-a-move-operation-should-move-and-leave-its-source-in-a-valid-state leave the moved-from object in valid state] (that is, a state with class invariants intact), and either [https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c65-make-move-assignment-safe-for-self-assignment do nothing] or at least leave the object in a valid state on self-assignment, and return the lhs by reference to non-const, and be noexcept: {{source|1= // move assignment T& operator=(T&& other) noexcept { // Guard self assignment if (this == &other) return *this; // delete[]/size=0 would also be ok delete[] mArray; // release resource in *this mArray = std::exchange(other.mArray, nullptr); // leave other in valid state size = std::exchange(other.size, 0); return *this; } }} }} In those situations where copy assignment cannot benefit from resource reuse (it does not manage a heap-allocated array and does not have a (possibly transitive) member that does, such as a member {{lc|std::vector}} or {{lc|std::string}}), there is a popular convenient shorthand: the copy-and-swap assignment operator, which takes its parameter by value (thus working as both copy- and move-assignment depending on the value category of the argument), swaps with the parameter, and lets the destructor clean it up. {{source|1= // copy assignment (copy-and-swap idiom) T& T::operator=(T other) noexcept // call copy or move constructor to construct other { std::swap(size, other.size); // exchange resources between *this and other std::swap(mArray, other.mArray); return *this; } // destructor of other is called to release the resources formerly managed by *this }} This form automatically provides {{rlp|exceptions#Exception safety|strong exception guarantee}}, but prohibits resource reuse. ====Stream extraction and insertion==== The overloads of {{tt|operator>>}} and {{tt|operator<<}} that take a {{c|std::istream&}} or {{c|std::ostream&}} as the left hand argument are known as insertion and extraction operators. Since they take the user-defined type as the right argument ({{tt|b}} in {{tt|''a @ b''}}), they must be implemented as non-members. {{source|1= std::ostream& operator<<(std::ostream& os, const T& obj) { // write obj to stream return os; } std::istream& operator>>(std::istream& is, T& obj) { // read obj from stream if (/* T could not be constructed */) is.setstate(std::ios::failbit); return is; } }} These operators are sometimes implemented as {{rlp|friend|friend functions}}. ====Function call operator==== When a user-defined class overloads the function call operator, {{c|operator()}}, it becomes a {{named req|FunctionObject}} type. An object of such a type can be used in a function call expression: {{source|1= // An object of this type represents a linear function of one variable a * x + b. struct Linear { double a, b; double operator()(double x) const { return a * x + b; } }; int main() { Linear f{2, 1}; // Represents function 2x + 1. Linear g{-1, 0}; // Represents function -x. // f and g are objects that can be used like a function. double f_0 = f(0); double f_1 = f(1); double g_0 = g(0); } }} Many standard algorithms, from {{c|std::sort}} to {{c|std::accumulate}} accept {{named req|FunctionObject}}s to customize behavior. There are no particularly notable canonical forms of {{c|operator()}}, but to illustrate the usage: {{example |code= #include <algorithm> #include <iostream> #include <vector> struct Sum { int sum = 0; void operator()(int n) { sum += n; } }; int main() { std::vector<int> v = {1, 2, 3, 4, 5}; Sum s = std::for_each(v.begin(), v.end(), Sum()); std::cout << "The sum is " << s.sum << '\n'; } |output= The sum is 15 }} ====Increment and decrement==== When the postfix increment or decrement operator appears in an expression, the corresponding user-defined function ({{c|operator++}} or {{c|operator--}}) is called with an integer argument {{tt|0}}. Typically, it is implemented as {{c|T operator++(int)}} or {{c|T operator--(int)}}, where the argument is ignored. The postfix increment and decrement operators are usually implemented in terms of the prefix versions: {{source|1= struct X { // prefix increment X& operator++() { // actual increment takes place here return *this; // return new value by reference } // postfix increment X operator++(int) { X old = *this; // copy old value operator++(); // prefix increment return old; // return old value } // prefix decrement X& operator--() { // actual decrement takes place here return *this; // return new value by reference } // postfix decrement X operator--(int) { X old = *this; // copy old value operator--(); // prefix decrement return old; // return old value } }; }} Although the canonical implementations of the prefix increment and decrement operators return by reference, as with any operator overload, the return type is user-defined; for example the overloads of these operators for {{lc|std::atomic}} return by value. ====Binary arithmetic operators==== Binary operators are typically implemented as non-members to maintain symmetry (for example, when adding a complex number and an integer, if {{tt|operator+}} is a member function of the complex type, then only {{c|complex + integer}} would compile, and not {{c|integer + complex}}). Since for every binary arithmetic operator there exists a corresponding compound assignment operator, canonical forms of binary operators are implemented in terms of their compound assignments: {{source|1= class X { public: X& operator+=(const X& rhs) // compound assignment (does not need to be a member, { // but often is, to modify the private members) /* addition of rhs to *this takes place here */ return *this; // return the result by reference } // friends defined inside class body are inline and are hidden from non-ADL lookup friend X operator+(X lhs, // passing lhs by value helps optimize chained a+b+c const X& rhs) // otherwise, both parameters may be const references { lhs += rhs; // reuse compound assignment return lhs; // return the result by value (uses move constructor) } }; }} ====Comparison operators==== Standard algorithms such as {{c|std::sort}} and containers such as {{c|std::set}} expect {{c|operator<}} to be defined, by default, for the user-provided types, and expect it to implement strict weak ordering (thus satisfying the {{named req|Compare}} requirements). An idiomatic way to implement strict weak ordering for a structure is to use lexicographical comparison provided by {{lc|std::tie}}: {{source|1= struct Record { std::string name; unsigned int floor; double weight; friend bool operator<(const Record& l, const Record& r) { return std::tie(l.name, l.floor, l.weight) < std::tie(r.name, r.floor, r.weight); // keep the same order } }; }} Typically, once {{c|operator<}} is provided, the other relational operators are implemented in terms of {{c|operator<}}. {{source|1= inline bool operator< (const X& lhs, const X& rhs) { /* do actual comparison */ } inline bool operator> (const X& lhs, const X& rhs) { return rhs < lhs; } inline bool operator<=(const X& lhs, const X& rhs) { return !(lhs > rhs); } inline bool operator>=(const X& lhs, const X& rhs) { return !(lhs < rhs); } }} Likewise, the inequality operator is typically implemented in terms of {{c|1=operator==}}: {{source|1= inline bool operator==(const X& lhs, const X& rhs) { /* do actual comparison */ } inline bool operator!=(const X& lhs, const X& rhs) { return !(lhs == rhs); } }} When three-way comparison (such as {{lc|std::memcmp}} or {{lc|std::string::compare}}) is provided, all six two-way comparison operators may be expressed through that: {{source|1= inline bool operator==(const X& lhs, const X& rhs) { return cmp(lhs,rhs) == 0; } inline bool operator!=(const X& lhs, const X& rhs) { return cmp(lhs,rhs) != 0; } inline bool operator< (const X& lhs, const X& rhs) { return cmp(lhs,rhs) < 0; } inline bool operator> (const X& lhs, const X& rhs) { return cmp(lhs,rhs) > 0; } inline bool operator<=(const X& lhs, const X& rhs) { return cmp(lhs,rhs) <= 0; } inline bool operator>=(const X& lhs, const X& rhs) { return cmp(lhs,rhs) >= 0; } }} {{rrev|since=c++20| The inequality operator is automatically generated by the compiler if {{c|1=operator==}} is defined. Likewise, the four relational operators are automatically generated by the compiler if the three-way comparison operator {{c|operator<{{=}}>}} is defined. {{c|1=operator==}} and {{c|1=operator!=}}, in turn, are generated by the compiler if {{c|operator<{{=}}>}} is defined as defaulted: {{source|1= struct Record { std::string name; unsigned int floor; double weight; auto operator<=>(const Record&) const = default; }; // records can now be compared with ==, !=, <, <=, >, and >= }} See {{rlp|default comparisons}} for details. }} ====Array subscript operator==== User-defined classes that provide array-like access that allows both reading and writing typically define two overloads for {{c|operator[]}}: const and non-const variants: {{source|1= struct T { value_t& operator[](std::size_t idx) { return mVector[idx]; } const value_t& operator[](std::size_t idx) const { return mVector[idx]; } }; }} {{rrev|since=c++23| Alternatively, they can be expressed as a single member function template using an {{rlp|member functions#Explicit object parameter|explicit object parameter}}: {{source|1= struct T { decltype(auto) operator[](this auto& self, std::size_t idx) { return self.mVector[idx]; } }; }} }} If the value type is known to be a scalar type, the const variant should return by value. Where direct access to the elements of the container is not wanted or not possible or distinguishing between lvalue {{c|1=c[i] = v;}} and rvalue {{c|1=v = c[i];}} usage, {{c|operator[]}} may return a proxy. See for example {{lc|std::bitset::operator[]}}. Because a subscript operator can only take one subscript until C++23, to provide multidimensional array access semantics, e.g. to implement a 3D array access {{c|1=a[i][j][k] = x;}}, {{c|operator[]}} has to return a reference to a 2D plane, which has to have its own {{c|operator[]}} which returns a reference to a 1D row, which has to have {{c|operator[]}} which returns a reference to the element. To avoid this complexity, some libraries opt for overloading {{c|operator()}} instead, so that 3D access expressions have the Fortran-like syntax {{c|1=a(i, j, k) = x;}}. {{rrev|since=c++23| {{c|operator[]}} can take any number of subscripts. For example, an {{c|operator[]}} of a 3D array class declared as {{c|T& operator[](std::size_t x, std::size_t y, std::size_t z);}} can directly access the elements. {{example |code= #include <array> #include <cassert> #include <iostream> template<typename T, std::size_t Z, std::size_t Y, std::size_t X> struct Array3d { std::array<T, X * Y * Z> m{}; constexpr T& operator[](std::size_t z, std::size_t y, std::size_t x) // C++23 { assert(x < X and y < Y and z < Z); return m[z * Y * X + y * X + x]; } }; int main() { Array3d<int, 4, 3, 2> v; v[3, 2, 1] = 42; std::cout << "v[3, 2, 1] = " << v[3, 2, 1] << '\n'; } |output= v[3, 2, 1] = 42 }} }} ====Bitwise arithmetic operators==== User-defined classes and enumerations that implement the requirements of {{named req|BitmaskType}} are required to overload the bitwise arithmetic operators {{c|operator&}}, {{c|operator{{!}}}}, {{c|operator^}}, {{c|operator~}}, {{c|operator&{{=}}}}, {{c|operator{{!}}{{=}}}}, and {{c|operator^{{=}}}}, and may optionally overload the shift operators {{c|operator<<}} {{c|operator>>}}, {{c|operator>>{{=}}}}, and {{c|operator<<{{=}}}}. The canonical implementations usually follow the pattern for binary arithmetic operators described above. ====Boolean negation operator==== {{rev begin}} {{rev|until=c++11| The operator {{c|operator!}} is commonly overloaded by the user-defined classes that are intended to be used in boolean contexts. Such classes also provide a user-defined conversion function to boolean type (see {{lc|std::basic_ios}} for the standard library example), and the expected behavior of {{c|operator!}} is to return the value opposite of {{c|operator bool}}. }} {{rev|since=c++11| Since the built-in operator {{c|!}} performs {{rlp|implicit conversion#Contextual conversions|contextual conversion to {{tt|bool}}}}, user-defined classes that are intended to be used in boolean contexts could provide only {{c|operator bool}} and need not overload {{c|operator!}}. }} {{rev end}} ====Rarely overloaded operators==== The following operators are rarely overloaded: * The address-of operator, {{c|operator&}}. If the unary & is applied to an lvalue of incomplete type and the complete type declares an overloaded {{c|operator&}}, it is unspecified whether the operator has the built-in meaning or the operator function is called. Because this operator may be overloaded, generic libraries use {{lc|std::addressof}} to obtain addresses of objects of user-defined types. The best known example of a canonical overloaded operator& is the Microsoft class [https://docs.microsoft.com/en-us/cpp/atl/reference/ccomptrbase-class?view=msvc-160#operator_amp {{tt|CComPtrBase}}]. An example of this operator's use in EDSL can be found in [https://www.boost.org/doc/libs/release/libs/spirit/doc/html/spirit/qi/reference/operator/and_predicate.html boost.spirit]. * The boolean logic operators, {{c|operator&&}} and {{c|operator{{!!}}}}. Unlike the built-in versions, the overloads cannot implement short-circuit evaluation. {{rev inl|until=c++17|Also unlike the built-in versions, they do not sequence their left operand before the right one.}} In the standard library, these operators are only overloaded for {{lc|std::valarray}}. * The comma operator, {{c|operator,}}. {{rev inl|until=c++17|Unlike the built-in version, the overloads do not sequence their left operand before the right one.}} Because this operator may be overloaded, generic libraries use expressions such as {{c|a,void(),b}} instead of {{c|a,b}} to sequence execution of expressions of user-defined types. The boost library uses {{c|operator,}} in [https://www.boost.org/doc/libs/release/libs/assign/doc/index.html#intro boost.assign], [https://github.com/boostorg/spirit/blob/develop/include/boost/spirit/home/qi/string/symbols.hpp#L317 boost.spirit], and other libraries. The database access library [https://soci.sourceforge.net/doc.html SOCI] also overloads {{c|operator,}}. * The member access through pointer to member {{c|operator->*}}. There are no specific downsides to overloading this operator, but it is rarely used in practice. It was suggested that it could be part of a [https://www.aristeia.com/Papers/DDJ_Oct_1999.pdf smart pointer interface], and in fact is used in that capacity by actors in [https://www.boost.org/doc/libs/release/libs/phoenix/doc/html/phoenix/modules/operator.html#phoenix.modules.operator.member_pointer_operator boost.phoenix]. It is more common in EDSLs such as [https://github.com/schlangster/cpp.react/blob/legacy1/include/react/Signal.h#L557 cpp.react]. ===Notes=== {{ftm begin|std=1|comment=1}} {{ftm|std=C++23|value=202207L|__cpp_static_call_operator|{{c|static operator()}}}} {{ftm|std=C++23|value=202211L|__cpp_multidimensional_subscript|{{c|static operator[]}}}} {{ftm end}} ===Example=== {{example |code= #include <iostream> class Fraction { // or C++17's std::gcd constexpr int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int n, d; public: constexpr Fraction(int n, int d = 1) : n(n / gcd(n, d)), d(d / gcd(n, d)) {} constexpr int num() const { return n; } constexpr int den() const { return d; } constexpr Fraction& operator*=(const Fraction& rhs) { int new_n = n * rhs.n / gcd(n * rhs.n, d * rhs.d); d = d * rhs.d / gcd(n * rhs.n, d * rhs.d); n = new_n; return *this; } }; std::ostream& operator<<(std::ostream& out, const Fraction& f) { return out << f.num() << '/' << f.den(); } constexpr bool operator==(const Fraction& lhs, const Fraction& rhs) { return lhs.num() == rhs.num() && lhs.den() == rhs.den(); } constexpr bool operator!=(const Fraction& lhs, const Fraction& rhs) { return !(lhs == rhs); } constexpr Fraction operator*(Fraction lhs, const Fraction& rhs) { return lhs *= rhs; } int main() { constexpr Fraction f1{3, 8}, f2{1, 2}, f3{10, 2}; std::cout << f1 << " * " << f2 << " = " << f1 * f2 << '\n' << f2 << " * " << f3 << " = " << f2 * f3 << '\n' << 2 << " * " << f1 << " = " << 2 * f1 << '\n'; static_assert(f3 == f2 * 10); } |output= 3/8 * 1/2 = 3/16 1/2 * 5/1 = 5/2 2 * 3/8 = 3/4 }} ===Defect reports=== {{dr list begin}} {{dr list item|wg=cwg|dr=1481|std=C++98|before=the non-member prefix increment operator could<br>only have a parameter of class or enumeration type|after=no type requirement}} {{dr list end}} ===See also=== * {{rlp|operator precedence|Operator precedence}} * {{rlp|operator alternative|Alternative operator syntax}} * {{rlp|adl|Argument-dependent lookup}} {{cpp/language/operators}} ===External links=== {{eli|<references/>}} {{langlinks|de|es|fr|it|ja|pt|ru|zh}} -->