operator==, !=, <, <=, >, >=, <=>(std::variant)
ja.cppreference.net より
| ヘッダ <variant>
|
||
template< class... Types >
constexpr bool operator==( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
で定義されます。 (1) | (C++17 以降) |
template< class... Types >
constexpr bool operator!=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(2) | (C++17 以降) |
template< class... Types >
constexpr bool operator<( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(3) | (C++17 以降) |
template< class... Types >
constexpr bool operator>( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(4) | (C++17 以降) |
template< class... Types >
constexpr bool operator<=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(5) | (C++17 以降) |
template< class... Types >
constexpr bool operator>=( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(6) | (C++17 以降) |
template< class... Types >
constexpr std::common_comparison_category_t
<std::compare_three_way_result_t<Types>...>
operator<=>( const std::variant<Types...>& lhs,
const std::variant<Types...>& rhs );
|
(7) | (C++20 以降) |
| ヘルパー関数テンプレート |
||
template< std::size_t I, class... Types >
constexpr const std::variant_alternative_t<I, std::variant<Types...>>&
GET( const variant<Types...>& v );
|
(8) | (説明専用*) |
std::variant オブジェクトに対して比較操作を実行します。
1-7) 2 つの std::variant オブジェクト
lhs と rhs を比較します。格納されている値は、T と lhs の両方が同じインデックスに対応する値を保持している場合にのみ、(rhs の対応する演算子を使用して) 比較されます。そうでない場合、
lhsは、等しいとrhsみなされるのは、lhsとrhsの両方が値を保持していない場合に限ります。lhsは、より小さいとrhsみなされるのは、rhsが値を保持しlhsが保持していない場合、またはlhs.index()がrhs.index()より小さい場合のいずれかです。
1-6) これらの関数のそれぞれについて、
@ を対応する比較演算子とします。
|
何らかの |
(C++26 まで) |
|
このオーバーロードは、すべての |
(C++26 以降) |
I < sizeof...(Types) が false である場合、プログラムは不正形式となります。I == v.index() が 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オブジェクトを比較する
(関数テンプレート) |