operator==, !=, <, <=, >, >=, <=>(std::optional)
From cppreference.com
| ヘッダ <optional> で定義 |
||
| 2つの optional オブジェクトを比較する |
||
| template< class T, class U > constexpr bool operator==( const optional<T>& lhs, const optional<U>& rhs ); |
(1) | (C++17以降) |
| template< class T, class U > constexpr bool operator!=( const optional<T>& lhs, const optional<U>& rhs ); |
(2) | (C++17以降) |
| template< class T, class U > constexpr bool operator<( const optional<T>& lhs, const optional<U>& rhs ); |
(3) | (C++17以降) |
| template< class T, class U > constexpr bool operator<=( const optional<T>& lhs, const optional<U>& rhs ); |
(4) | (C++17以降) |
| template< class T, class U > constexpr bool operator>( const optional<T>& lhs, const optional<U>& rhs ); |
(5) | (C++17以降) |
| template< class T, class U > constexpr bool operator>=( const optional<T>& lhs, const optional<U>& rhs ); |
(6) | (C++17以降) |
| template< class T, std::three_way_comparable_with<T> U > constexpr std::compare_three_way_result_t<T, U> |
(7) | (C++20以降) |
optional オブジェクトを nullopt と比較する |
||
| template< class T > constexpr bool operator==( const optional<T>& opt, std::nullopt_t ) noexcept; |
(8) | (C++17以降) |
| template< class T > constexpr bool operator==( std::nullopt_t, const optional<T>& opt ) noexcept; |
(9) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator!=( const optional<T>& opt, std::nullopt_t ) noexcept; |
(10) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator!=( std::nullopt_t, const optional<T>& opt ) noexcept; |
(11) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator<( const optional<T>& opt, std::nullopt_t ) noexcept; |
(12) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator<( std::nullopt_t, const optional<T>& opt ) noexcept; |
(13) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator<=( const optional<T>& opt, std::nullopt_t ) noexcept; |
(14) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator<=( std::nullopt_t, const optional<T>& opt ) noexcept; |
(15) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator>( const optional<T>& opt, std::nullopt_t ) noexcept; |
(16) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator>( std::nullopt_t, const optional<T>& opt ) noexcept; |
(17) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator>=( const optional<T>& opt, std::nullopt_t ) noexcept; |
(18) | (C++17以降) (C++20まで) |
| template< class T > constexpr bool operator>=( std::nullopt_t, const optional<T>& opt ) noexcept; |
(19) | (C++17以降) (C++20まで) |
| template< class T > constexpr std::strong_ordering |
(20) | (C++20以降) |
optional オブジェクトを値と比較する |
||
| template< class T, class U > constexpr bool operator==( const optional<T>& opt, const U& value ); |
(21) | (C++17以降) |
| template< class U, class T > constexpr bool operator==( const U& value, const optional<T>& opt ); |
(22) | (C++17以降) |
| template< class T, class U > constexpr bool operator!=( const optional<T>& opt, const U& value ); |
(23) | (C++17以降) |
| template< class U, class T > constexpr bool operator!=( const U& value, const optional<T>& opt ); |
(24) | (C++17以降) |
| template< class T, class U > constexpr bool operator<( const optional<T>& opt, const U& value ); |
(25) | (C++17以降) |
| template< class U, class T > constexpr bool operator<( const U& value, const optional<T>& opt ); |
(26) | (C++17以降) |
| template< class T, class U > constexpr bool operator<=( const optional<T>& opt, const U& value ); |
(27) | (C++17以降) |
| template< class U, class T > constexpr bool operator<=( const U& value, const optional<T>& opt ); |
(28) | (C++17以降) |
| template< class T, class U > constexpr bool operator>( const optional<T>& opt, const U& value ); |
(29) | (C++17以降) |
| template< class U, class T > constexpr bool operator>( const U& value, const optional<T>& opt ); |
(30) | (C++17以降) |
| template< class T, class U > constexpr bool operator>=( const optional<T>& opt, const U& value ); |
(31) | (C++17以降) |
| template< class U, class T > constexpr bool operator>=( const U& value, const optional<T>& opt ); |
(32) | (C++17以降) |
| template< class T, std::three_way_comparable_with<T> U > constexpr std::compare_three_way_result_t<T, U> |
(33) | (C++20以降) |
optional オブジェクトに対する比較演算を実行する。
1-7) 2つの
optional オブジェクト lhs と rhs を比較する。lhs と rhs の両方が値を持っている場合にのみ、格納された値が比較される(T の対応する演算子を使用)。そうでない場合、- lhs は、lhs と rhs の両方が値を含んでいない場合に限り、rhs と等しいと見なされます。
- rhs が値を含み、lhs が値を含まない場合に限り、lhs は rhs より小さいとみなされる。
1-6) これらの各関数について、@ を対応する比較演算子とする
|
対応する式 *lhs @ *rhs が不正な形式であるか、その結果が bool に変換できない場合、プログラムは不正な形式となる。 |
(C++26まで) |
|
このオーバーロードは、対応する式 *lhs @ *rhs が整形式であり、その結果が bool に変換できる場合にのみ、オーバーロード解決に参加する。 |
(C++26以降) |
8-20) opt を
nullopt と比較する。値を含まない optional と比較する場合の (1-6) と同等。|
|
(C++20以降) |
21-33) opt を value と比較する。opt が値を持っている場合にのみ、値が比較される(
T の対応する演算子を使用)。そうでない場合、opt は value より小さいとみなされる。21-32) これらの各関数について、@ を対応する比較演算子とする
|
対応する式 *opt @ value または value @ *opt (オペランドの位置による) が不正な形式であるか、その結果が bool に変換できない場合、プログラムは不正な形式となる。 |
(C++26まで) |
|
このオーバーロードは、以下のすべての条件が満たされた場合にのみオーバーロード解決に参加する。
|
(C++26以降) |
目次 |
[編集] パラメータ
| lhs, rhs, opt | - | 比較する optional オブジェクト |
| value | - | 格納された値と比較する値 |
[編集] 戻り値
1) lhs.has_value() != rhs.has_value() ? false :
(lhs.has_value() == false ? true : *lhs == *rhs)
(lhs.has_value() == false ? true : *lhs == *rhs)
2) lhs.has_value() != rhs.has_value() ? true :
(lhs.has_value() == false ? false : *lhs != *rhs)
(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
[編集] 例外
1-7) 実装定義の例外を投げる場合がある。
21-33) 比較が例外を投げる場合に、その例外を投げる。
[編集] 備考
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_constrained_equality |
202403L |
(C++26) | std::optional の制約付き比較演算子 |
[編集] 欠陥報告
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2945 | C++17 | T と比較するケースでテンプレートパラメータの順序が不整合 | 整合性が取れた |