operator==, !=, <, <=, >, >=, <=> (std::variant)
|
ヘッダーで定義
<variant>
|
||
|
template
<
class
...
Types
>
constexpr
bool
operator
==
(
const
std::
variant
<
Types...
>
&
lhs,
|
(1) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
bool
operator
!
=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(2) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
bool
operator
<
(
const
std::
variant
<
Types...
>
&
lhs,
|
(3) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
bool
operator
>
(
const
std::
variant
<
Types...
>
&
lhs,
|
(4) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
bool
operator
<=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(5) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
bool
operator
>=
(
const
std::
variant
<
Types...
>
&
lhs,
|
(6) | (C++17以降) |
|
template
<
class
...
Types
>
constexpr
std::
common_comparison_category_t
|
(7) | (C++20以降) |
|
ヘルパー関数テンプレート
|
||
|
template
<
std::
size_t
I,
class
...
Types
>
constexpr
const
std::
variant_alternative_t
<
I,
std::
variant
<
Types...
>>
&
|
(8) | ( 説明専用* ) |
std::variant オブジェクトに対する比較操作を実行します。
T
の対応する演算子を使用)。それ以外の場合、
- lhs が rhs と 等しい と見なされるのは、 lhs と rhs の両方が値を含んでいない場合に限る。
- lhs が rhs より 小さい と見なされるのは、 rhs が値を含み lhs が値を含まない場合、または lhs. index ( ) が rhs. index ( ) より小さい場合に限る。
|
I
の一部の値について、対応する式
|
(C++26未満) |
|
このオーバーロードは、すべての
I
の値について、対応する式
|
(C++26以降) |
I
が
I
<
sizeof...
(
Types
)
の場合、
false
であれば、プログラムは不適格です。
目次 |
パラメータ
| lhs,rhs | - | 比較対象のバリアント |
戻り値
| 演算子 |
両オペランドが値を保持する場合
( I を lhs. index ( ) 、 J を rhs. index ( ) とする) |
lhs
または
rhs
が値を保持しない場合
( lhs_empty を lhs. valueless_by_exception ( ) 、 rhs_empty を rhs. valueless_by_exception ( ) とする) |
|
|---|---|---|---|
| I と J は等しい | I と J は等しくない | ||
| == |
GET
<
I
>
(
lhs
)
==
GET
<
I
>
(
rhs
)
|
false | lhs_empty && rhs_empty |
| ! = |
GET
<
I
>
(
lhs
)
!
=
GET
<
I
>
(
rhs
)
|
true | lhs_empty ! = rhs_empty |
| < |
GET
<
I
>
(
lhs
)
<
GET
<
I
>
(
rhs
)
|
lhs. index ( ) < rhs. index ( ) | lhs_empty && ! rhs_empty |
| > |
GET
<
I
>
(
lhs
)
>
GET
<
I
>
(
rhs
)
|
lhs. index ( ) > rhs. index ( ) | ! lhs_empty && rhs_empty |
| <= |
GET
<
I
>
(
lhs
)
<=
GET
<
I
>
(
rhs
)
|
lhs. index ( ) < rhs. index ( ) | lhs_empty |
| >= |
GET
<
I
>
(
lhs
)
>=
GET
<
I
>
(
rhs
)
|
lhs. index ( ) > rhs. index ( ) | rhs_empty |
| <=> |
GET
<
I
>
(
lhs
)
<=>
GET
<
I
>
(
rhs
)
|
lhs. index ( ) <=> rhs. index ( ) | 以下を参照 |
operator <=> 演算子について:
- lhs のみが値を持たない場合、 std::strong_ordering::less を返す。
- rhs のみが値を持たない場合、 std::strong_ordering::greater を返す。
- lhs と rhs の両方が値を持たない場合、 std::strong_ordering::equal を返す。
注記
| 機能テスト マクロ | 値 | 標準 | 機能 |
|---|---|---|---|
__cpp_lib_constrained_equality
|
202403L
|
(C++26) | std::variant の制約付き比較演算子 |
例
#include <iostream> #include <string> #include <variant> int main() { std::cout << std::boolalpha; std::string cmp; bool result; auto print2 = [&cmp, &result](const auto& lhs, const auto& rhs) { std::cout << lhs << ' ' << cmp << ' ' << rhs << " : " << result << '\n'; }; std::variant<int, std::string> v1, v2; std::cout << "operator==\n"; { cmp = "=="; // デフォルトでは v1 = 0, v2 = 0; result = v1 == v2; // true std::visit(print2, v1, v2); v1 = v2 = 1; result = v1 == v2; // true std::visit(print2, v1, v2); v2 = 2; result = v1 == v2; // false std::visit(print2, v1, v2); v1 = "A"; result = v1 == v2; // false: v1.index == 1, v2.index == 0 std::visit(print2, v1, v2); v2 = "B"; result = v1 == v2; // false std::visit(print2, v1, v2); v2 = "A"; result = v1 == v2; // true std::visit(print2, v1, v2); } std::cout << "operator<\n"; { cmp = "<"; v1 = v2 = 1; result = v1 < v2; // false std::visit(print2, v1, v2); v2 = 2; result = v1 < v2; // true std::visit(print2, v1, v2); v1 = 3; result = v1 < v2; // false std::visit(print2, v1, v2); v1 = "A"; v2 = 1; result = v1 < v2; // false: v1.index == 1, v2.index == 0 std::visit(print2, v1, v2); v1 = 1; v2 = "A"; result = v1 < v2; // true: v1.index == 0, v2.index == 1 std::visit(print2, v1, v2); v1 = v2 = "A"; result = v1 < v2; // false std::visit(print2, v1, v2); v2 = "B"; result = v1 < v2; // true std::visit(print2, v1, v2); v1 = "C"; result = v1 < v2; // false std::visit(print2, v1, v2); } { std::variant<int, std::string> v1; std::variant<std::string, int> v2; // v1 == v2; // コンパイルエラー: 既知の変換が存在しない } // TODO: C++20 の三方比較演算子 <=> を variant に適用 }
出力:
operator== 0 == 0 : true 1 == 1 : true 1 == 2 : false A == 2 : false A == B : false A == A : true operator< 1 < 1 : false 1 < 2 : true 3 < 2 : false A < 1 : false 1 < A : true A < A : false A < B : true C < B : false
関連項目
|
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
|
optional
オブジェクトを比較する
(関数テンプレート) |