Namensräume
Varianten
Aktionen

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

Von cppreference.com
< cpp‎ | utility‎ | optional
 
 
Dienstprogramm-Bibliotheken
Sprachunterstützung
Typunterstützung (Basistypen, RTTI)
Bibliotheks-Feature-Test-Makros (C++20)
Programm-Dienstprogramme
Variadische Funktionen
Coroutine-Unterstützung (C++20)
Vertragsunterstützung (C++26)
Drei-Wege-Vergleich
(C++20)
(C++20)(C++20)(C++20)  
(C++20)(C++20)(C++20)

Allgemeine Hilfsmittel
Relationale Operatoren (in C++20 veraltet)
 
 
Definiert in der Header-Datei <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 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 (mittels des entsprechenden Operators von T) verglichen, nur wenn sowohl lhs als auch rhs Werte enthalten. Andernfalls gilt:
  • lhs ist *gleich* rhs genau dann, wenn weder lhs noch rhs einen Wert enthalten.
  • lhs ist *kleiner als* rhs genau dann, wenn rhs einen Wert enthält und lhs nicht.
1-6) Sei @ der entsprechende Vergleichsoperator. Für jede dieser Funktionen gilt:

Wenn der entsprechende Ausdruck *lhs @ *rhs ill-formed ist oder sein Ergebnis nicht in bool konvertierbar ist, ist das Programm ill-formed.

(bis C++26)

Diese Überladung nimmt nur an der Overload-Auflösung teil, wenn der entsprechende Ausdruck *lhs @ *rhs gut gebildet ist und sein Ergebnis in bool konvertierbar ist.

(seit C++26)
8-20) Vergleicht opt mit nullopt. Äquivalent zu (1-6), wenn mit einem optional verglichen wird, das keinen Wert enthält.

Die Operatoren <, <=, >, >= und != sind synthetisiert aus operator<=> und operator== beziehungsweise.

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

Wenn der entsprechende Ausdruck *opt @ value oder value @ *opt (je nach Position der Operanden) ill-formed ist oder sein Ergebnis nicht in bool konvertierbar ist, ist das Programm ill-formed.

(bis C++26)

Dieser Überladung nimmt nur 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 (je nach Position der Operanden) ist gut gebildet und sein Ergebnis ist in bool konvertierbar.
(seit C++26)

Inhalt

[bearbeiten] Parameter

lhs, rhs, opt - ein zu vergleichendes optional-Objekt
value - ein Wert, mit dem der enthaltene Wert verglichen wird

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

[bearbeiten] Ausnahmen

1-7) Können implementierungsabhängige Ausnahmen auslösen.
21-33) Wirft, wann und was der Vergleich wirft.

[bearbeiten] Hinweise

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

[bearbeiten] Fehlerberichte

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

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