Namespaces
Variants

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

From cppreference.net
Utilities library
ヘッダーで定義 <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 >

operator <=> ( const optional < T > & lhs, const optional < U > & rhs ) ;
(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

operator <=> ( const optional < T > & opt, std:: nullopt_t ) noexcept ;
(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 >

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

optional オブジェクトに対する比較操作を実行します。

1-7) 2つの optional オブジェクト、 lhs rhs を比較する。格納された値は、 lhs rhs の両方が値を保持している場合にのみ( T の対応する演算子を使用して)比較される。それ以外の場合、
  • lhs は、 lhs rhs の両方が値を保持していない場合に限り、 rhs 等しい と見なされる。
  • lhs は、 rhs が値を保持しており、 lhs が値を保持していない場合に限り、 rhs より 小さい と見なされる。
1-6) 各関数について、 @ を対応する比較演算子とする:

対応する式 * lhs @ * rhs が不適格であるか、その結果が bool に変換できない場合、プログラムは不適格となる。

(C++26まで)

このオーバーロードは、対応する式 * lhs @ * rhs が適格であり、かつその結果が bool に変換可能な場合にのみ、オーバーロード解決に参加する。

(C++26以降)
8-20) opt nullopt と比較する。値を含まない optional との比較における (1-6) と等価。

< <= > >= 、および != 演算子は、それぞれ operator <=> operator == から 合成される

(C++20以降)
21-33) opt value と比較します。値の比較は( T の対応する演算子を使用して) opt が値を含む場合にのみ行われます。それ以外の場合、 opt より小さい と見なされます value よりも。
21-32) @ を対応する比較演算子とし、以下の各関数について:

対応する式 * opt @ value または value @ * opt (オペランドの位置による)が不適格であるか、その結果が bool に変換できない場合、プログラムは不適格である。

(C++26まで)

このオーバーロードは、以下の条件が全て満たされる場合にのみオーバーロード解決に参加する:

  • U std::optional の特殊化ではないこと。
  • 対応する式 * opt @ value または value @ * opt (オペランドの位置による)が適格であり、その結果が bool に変換可能であること。
(C++26以降)

目次

パラメータ

lhs, rhs, opt - 比較対象の optional オブジェクト
value - 格納されている値と比較する値

戻り値

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

例外

1-7) 実装定義の例外をスローする可能性があります。
21-33) 比較が例外をスローする場合とその内容。

注記

機能テスト マクロ 標準 機能
__cpp_lib_constrained_equality 202403L (C++26) std::optional の制約付き比較演算子

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2945 C++17 compare-with-Tの場合のテンプレートパラメータ順序が一貫していない 一貫性を持たせた