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 起) | 
| 目录 | 
[编辑] 参数
| 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对象(函数模板) | 


