Namespaces
Variants

operator==, !=, <, <=, >, >=, <=> (std::optional)

From cppreference.net
Utilities library
Definiert im Header <optional>
Vergleiche zwei optional Objekte
template < class T, class U >
constexpr bool operator == ( const optional < T > & lhs, const optional < U > & rhs ) ;
(1) (seit C++17)
template < class T, class U >
constexpr bool operator ! = ( const optional < T > & lhs, const optional < U > & rhs ) ;
(2) (seit C++17)
template < class T, class U >
constexpr bool operator < ( const optional < T > & lhs, const optional < U > & rhs ) ;
(3) (seit C++17)
template < class T, class U >
constexpr bool operator <= ( const optional < T > & lhs, const optional < U > & rhs ) ;
(4) (seit C++17)
template < class T, class U >
constexpr bool operator > ( const optional < T > & lhs, const optional < U > & rhs ) ;
(5) (seit C++17)
template < class T, class U >
constexpr bool operator >= ( const optional < T > & lhs, const optional < U > & rhs ) ;
(6) (seit C++17)
template < class T, std:: three_way_comparable_with < T > U >

constexpr std:: compare_three_way_result_t < T, U >

operator <=> ( const optional < T > & lhs, const optional < U > & rhs ) ;
(7) (seit C++20)
Vergleiche ein optional -Objekt mit einem nullopt
template < class T >
constexpr bool operator == ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(8) (seit C++17)
template < class T >
constexpr bool operator == ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(9) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator ! = ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(10) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator ! = ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(11) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator < ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(12) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator < ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(13) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator <= ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(14) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator <= ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(15) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator > ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(16) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator > ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(17) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator >= ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(18) (seit C++17)
(bis C++20)
template < class T >
constexpr bool operator >= ( std:: nullopt_t , const optional < T > & opt ) noexcept ;
(19) (seit C++17)
(bis C++20)
template < class T >

constexpr std:: strong_ordering

operator <=> ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(20) (seit C++20)
Vergleiche ein optional -Objekt mit einem Wert
template < class T, class U >
constexpr bool operator == ( const optional < T > & opt, const U & value ) ;
(21) (seit C++17)
template < class U, class T >
constexpr bool operator == ( const U & value, const optional < T > & opt ) ;
(22) (seit C++17)
template < class T, class U >
constexpr bool operator ! = ( const optional < T > & opt, const U & value ) ;
(23) (seit C++17)
template < class U, class T >
constexpr bool operator ! = ( const U & value, const optional < T > & opt ) ;
(24) (seit C++17)
template < class T, class U >
constexpr bool operator < ( const optional < T > & opt, const U & value ) ;
(25) (seit C++17)
template < class U, class T >
constexpr bool operator < ( const U & value, const optional < T > & opt ) ;
(26) (seit C++17)
template < class T, class U >
constexpr bool operator <= ( const optional < T > & opt, const U & value ) ;
(27) (seit C++17)
template < class U, class T >
constexpr bool operator <= ( const U & value, const optional < T > & opt ) ;
(28) (seit C++17)
template < class T, class U >
constexpr bool operator > ( const optional < T > & opt, const U & value ) ;
(29) (seit C++17)
template < class U, class T >
constexpr bool operator > ( const U & value, const optional < T > & opt ) ;
(30) (seit C++17)
template < class T, class U >
constexpr bool operator >= ( const optional < T > & opt, const U & value ) ;
(31) (seit C++17)
template < class U, class T >
constexpr bool operator >= ( const U & value, const optional < T > & opt ) ;
(32) (seit C++17)
template < class T, std:: three_way_comparable_with < T > U >

constexpr std:: compare_three_way_result_t < T, U >

operator <=> ( const optional < T > & opt, const U & value ) ;
(33) (seit C++20)

Führt Vergleichsoperationen auf optional -Objekten aus.

1-7) Vergleicht zwei optional -Objekte, lhs und rhs . Die enthaltenen Werte werden nur dann verglichen (unter Verwendung des entsprechenden Operators von T ), wenn sowohl lhs als auch rhs Werte enthalten. Andernfalls gilt:
  • lhs wird als gleich rhs betrachtet, genau dann, wenn sowohl lhs als auch rhs keinen Wert enthalten.
  • lhs wird als kleiner als rhs betrachtet, genau dann, wenn rhs einen Wert enthält und lhs keinen.
1-6) Sei @ der entsprechende Vergleichsoperator, für jede dieser Funktionen:

Falls der entsprechende Ausdruck * lhs @ * rhs ungültig ist oder sein Ergebnis nicht in bool konvertierbar ist, ist das Programm ungültig.

(bis C++26)

Diese Überladung nimmt nur an der Überladungsauflösung teil, wenn der entsprechende Ausdruck * lhs @ * rhs gültig ist und sein Ergebnis in bool konvertierbar ist.

(seit C++26)
8-20) Vergleicht opt mit einem nullopt . Entspricht (1-6) beim Vergleich mit einem optional , das keinen Wert enthält.

Die < -, <= -, > -, >= - und != -Operatoren werden synthetisiert aus operator <=> bzw. operator == .

(since C++20)
21-33) Vergleicht opt mit einem value . Die Werte werden nur verglichen (unter Verwendung des entsprechenden Operators von T ), wenn opt einen Wert enthält. Andernfalls wird opt als kleiner als value betrachtet.
21-32) Sei @ der entsprechende Vergleichsoperator, für jede dieser Funktionen:

Falls der entsprechende Ausdruck * opt @ value oder value @ * opt (abhängig von der Position der Operanden) fehlerhaft ist oder sein Ergebnis nicht in bool konvertierbar ist, ist das Programm fehlerhaft.

(bis C++26)

Diese Überladung nimmt nur dann an der Überladungsauflösung teil, wenn alle folgenden Bedingungen erfüllt sind:

  • U ist keine Spezialisierung von std::optional .
  • Der entsprechende Ausdruck * opt @ value oder value @ * opt (abhängig von der Position der Operanden) ist wohlgeformt und sein Ergebnis ist in bool konvertierbar.
(seit C++26)

Inhaltsverzeichnis

Parameter

lhs, rhs, opt - ein optional Objekt zum Vergleichen
value - Wert zum Vergleich mit dem enthaltenen Wert

Rückgabewert

1) lhs. has_value ( ) ! = rhs. has_value ( ) ? false :
( lhs. has_value ( ) == false ? true : * lhs == * rhs )
2) lhs. has_value ( ) ! = rhs. has_value ( ) ? true :
( lhs. has_value ( ) == false ? false : * lhs ! = * rhs )
3) ! rhs ? false : ( ! lhs ? true : * lhs < * rhs )
4) ! lhs ? true : ( ! rhs ? false : * lhs <= * rhs )
5) ! lhs ? false : ( ! rhs ? true : * lhs > * rhs )
6) ! rhs ? true : ( ! lhs ? false : * lhs >= * rhs )
7) lhs && rhs ? * lhs <=> * rhs : lhs. has_value ( ) <=> rhs. has_value ( )
8,9) ! opt
10,11) opt. has_value ( )
12) false
13) opt. has_value ( )
14) ! opt
15) true
16) opt. has_value ( )
17) false
18) true
19) ! opt
20) opt. has_value ( ) <=> false
21) opt. has_value ( ) ? * opt == value : false
22) opt. has_value ( ) ? value == * opt : false
23) opt. has_value ( ) ? * opt ! = value : true
24) opt. has_value ( ) ? value ! = * opt : true
25) opt. has_value ( ) ? * opt < value : true
26) opt. has_value ( ) ? value < * opt : false
27) opt. has_value ( ) ? * opt <= value : true
28) opt. has_value ( ) ? value <= * opt : false
29) opt. has_value ( ) ? * opt > value : false
30) opt. has_value ( ) ? value > * opt : true
31) opt. has_value ( ) ? * opt >= value : false
32) opt. has_value ( ) ? value >= * opt : true
33) opt. has_value ( ) ? * opt <=> value : std :: strong_ordering :: less

Exceptions

1-7) Kann implementierungsdefinierte Ausnahmen werfen.
21-33) Wirft aus, wann und was der Vergleich auswirft.

Hinweise

Feature-Test Makro Wert Std Funktion
__cpp_lib_constrained_equality 202403L (C++26) Eingeschränkte Vergleichsoperatoren für std::optional

Fehlerberichte

Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.

DR Angewendet auf Verhalten wie veröffentlicht Korrektes Verhalten
LWG 2945 C++17 Reihenfolge der Template-Parameter inkonsistent für Compare-with-T-Fälle konsistent gemacht