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 オブジェクトに対して比較演算を実行します。
std::variant オブジェクト lhs と rhs を比較します。含まれる値は、lhs と rhs の両方が同じインデックスに対応する値を含んでいる場合にのみ比較されます (T の対応する演算子を使用)。そうでない場合、- lhs は、lhs と rhs の両方が値を含んでいない場合に限り、rhs と等しいと見なされます。
- lhs は、rhs が値を持ち lhs が持たない場合、または lhs.index() が rhs.index() より小さい場合に限り、rhs より小さいと見なされます。
|
ある I の値について、対応する式 |
(C++26まで) |
|
このオーバーロードは、すべての I の値について、対応する式 |
(C++26以降) |
目次 |
[編集] パラメータ
| 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 = "=="; // by default 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; // Compilation error: no known conversion } // TODO: C++20 three-way comparison operator <=> for variants }
出力
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 オブジェクトを比較する(function template) |