Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/reference wrapper/operator cmp"

From cppreference.com
m (Notes)
m (Notes: +FTM.)
 
(One intermediate revision by one user not shown)
Line 4: Line 4:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl|num=1|since=c++26|1=
 
{{dcl|num=1|since=c++26|1=
friend constexpr bool  
+
friend constexpr bool
 
     operator==( reference_wrapper lhs, reference_wrapper rhs );
 
     operator==( reference_wrapper lhs, reference_wrapper rhs );
 
}}
 
}}
 
{{dcl|num=2|since=c++26|1=
 
{{dcl|num=2|since=c++26|1=
friend constexpr bool  
+
friend constexpr bool
 
     operator==( reference_wrapper lhs, reference_wrapper<const T> rhs );
 
     operator==( reference_wrapper lhs, reference_wrapper<const T> rhs );
 
}}
 
}}
 
{{dcl|num=3|since=c++26|1=
 
{{dcl|num=3|since=c++26|1=
friend constexpr bool  
+
friend constexpr bool
 
     operator==( reference_wrapper lhs, const T& ref );
 
     operator==( reference_wrapper lhs, const T& ref );
 
}}
 
}}
{{dcl|num=4|since=c++26|1=
+
{{dcla|num=4|since=c++26|1=
 
friend constexpr auto
 
friend constexpr auto
 
     operator<=>( reference_wrapper lhs, reference_wrapper rhs );
 
     operator<=>( reference_wrapper lhs, reference_wrapper rhs );
Line 32: Line 32:
  
 
@1,2@ Compares two {{tt|reference_wrapper}} objects. The objects compare equal if and only if {{c|lhs.get()}} and {{c|rhs.get()}} are equal.
 
@1,2@ Compares two {{tt|reference_wrapper}} objects. The objects compare equal if and only if {{c|lhs.get()}} and {{c|rhs.get()}} are equal.
* Both overloads participate in overload resolution only if {{c|1=lhs.get() == rhs.get()}} is well-formed and its result is convertible to {{c|bool}}.
+
* Both overloads participate in overload resolution only if {{c|1=lhs.get() == rhs.get()}} is well-formed and its result is convertible to {{c/core|bool}}.
 
* The overload {{v|2}} participates in overload resolution only if {{c|std::is_const_v<T>}} is {{c|false}}.
 
* The overload {{v|2}} participates in overload resolution only if {{c|std::is_const_v<T>}} is {{c|false}}.
  
 
@3@ Compares {{tt|reference_wrapper}} object with a reference. The parameters compare equal if and only if {{c|lhs.get()}} is equal to {{c|ref}}.
 
@3@ Compares {{tt|reference_wrapper}} object with a reference. The parameters compare equal if and only if {{c|lhs.get()}} is equal to {{c|ref}}.
* The overload {{v|3}} participates in overload resolution only if {{c|1=lhs.get() == ref}} is well-formed and its result is convertible to {{c|bool}}.
+
* The overload {{v|3}} participates in overload resolution only if {{c|1=lhs.get() == ref}} is well-formed and its result is convertible to {{c/core|bool}}.
  
 
@4@ Compares two {{tt|reference_wrapper}} objects as if by {{box|{{lti|cpp/standard library/synth-three-way}}{{c/core|1=(lhs.get(), rhs.get())}}}}. The return type is {{box|{{lti|cpp/standard library/synth-three-way|synth-three-way-result}}<T>}}. {{cpp/enable if|the return type is well-formed}}.
 
@4@ Compares two {{tt|reference_wrapper}} objects as if by {{box|{{lti|cpp/standard library/synth-three-way}}{{c/core|1=(lhs.get(), rhs.get())}}}}. The return type is {{box|{{lti|cpp/standard library/synth-three-way|synth-three-way-result}}<T>}}. {{cpp/enable if|the return type is well-formed}}.
Line 62: Line 62:
  
 
===Notes===
 
===Notes===
The return types of {{v|4,5,6}} are [[cpp/language/template argument deduction#auto-returning functions|deduced from {{tt|return}} statements]] to avoid hard error when instantiating a {{c|std::reference_wrapper<T>}} with {{box|{{lti|cpp/standard library/synth-three-way|synth-three-way-result}}<T>}} being ill-formed.
+
The return types of {{vl|4-6}} are [[cpp/language/template argument deduction#auto-returning functions|deduced from {{tt|return}} statements]] to avoid hard error when instantiating a {{c|std::reference_wrapper<T>}} with {{box|{{lti|cpp/standard library/synth-three-way|synth-three-way-result}}<T>}} being ill-formed.
 +
 
 +
{{feature test macro|__cpp_lib_reference_wrapper|Comparisons for {{tt|std::reference_wrapper}}|value=202403L|std=C++26}}
  
 
===Example===
 
===Example===

Latest revision as of 02:22, 8 July 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
 
friend constexpr bool
    operator==( reference_wrapper lhs, reference_wrapper rhs );
(1) (since C++26)
friend constexpr bool
    operator==( reference_wrapper lhs, reference_wrapper<const T> rhs );
(2) (since C++26)
friend constexpr bool
    operator==( reference_wrapper lhs, const T& ref );
(3) (since C++26)
friend constexpr auto
    operator<=>( reference_wrapper lhs, reference_wrapper rhs );
(4) (since C++26)
friend constexpr auto
    operator<=>( reference_wrapper lhs, reference_wrapper<const T> rhs );
(5) (since C++26)
friend constexpr auto
    operator<=>( reference_wrapper lhs, const T& ref );
(6) (since C++26)

Performs comparison operations on reference_wrapper objects.

1,2) Compares two reference_wrapper objects. The objects compare equal if and only if lhs.get() and rhs.get() are equal.
  • Both overloads participate in overload resolution only if lhs.get() == rhs.get() is well-formed and its result is convertible to bool.
  • The overload (2) participates in overload resolution only if std::is_const_v<T> is false.
3) Compares reference_wrapper object with a reference. The parameters compare equal if and only if lhs.get() is equal to ref.
  • The overload (3) participates in overload resolution only if lhs.get() == ref is well-formed and its result is convertible to bool.
4) Compares two reference_wrapper objects as if by synth-three-way(lhs.get(), rhs.get()). The return type is synth-three-way-result<T>. This overload participates in overload resolution only if the return type is well-formed.
5) Compares two reference_wrapper objects as if by synth-three-way(lhs.get(), rhs.get()). The return type is synth-three-way-result<T>. This overload participates in overload resolution only if std::is_const_v<T> is false and the return type is well-formed.
6) Compares reference_wrapper object with a reference as if by synth-three-way(lhs.get(), ref). The return type is synth-three-way-result<T>. This overload participates in overload resolution only if the return type is well-formed.

The <, <=, >, >=, and != operators are synthesized from operator<=> and operator== respectively.

Contents

[edit] Parameters

lhs, rhs - reference_wrapper object to compare
ref - reference to compare to the reference_wrapper object

[edit] Return value

1,2) lhs.get() == rhs.get().
3) lhs.get() == ref.
4,5) synth-three-way(lhs.get(), rhs.get()).
6) synth-three-way(lhs.get(), ref).

[edit] Exceptions

Throws when and what the comparison throws.

[edit] Notes

The return types of (4-6) are deduced from return statements to avoid hard error when instantiating a std::reference_wrapper<T> with synth-three-way-result<T> being ill-formed.

Feature-test macro Value Std Feature
__cpp_lib_reference_wrapper 202403L (C++26) Comparisons for std::reference_wrapper

[edit] Example