operator==, !=, <, <=, >, >=, <=>(std::optional)
來自 cppreference.com
定義於標頭檔案 <optional> |
||
比較兩個 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) 比較兩個
optional
物件 lhs 和 rhs。僅當 lhs 和 rhs 都包含值時,才比較它們所包含的值(使用 T
對應的運算子)。否則,- 當且僅當 lhs 和 rhs 都不包含值時,lhs 被認為是等於 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2945 | C++17 | 與 T 比較的模板引數順序不一致 | 已使其保持一致 |