Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | variant
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)
Swap and type operations
(C++14)
(C++11)

(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

Elementary string conversions
(C++17)
(C++17)
 
std::variant
Member functions
Observers
Modifiers
Non-member functions
operator==operator!=operator<operator<=operator>operator>=operator<=>
(C++20)
Helper classes
Helper objects
 
Defined in header <variant>
template< class... Types >

constexpr bool operator==( const std::variant<Types...>& v,

                           const std::variant<Types...>& w );
(1) (since C++17)
template< class... Types >

constexpr bool operator!=( const std::variant<Types...>& v,

                           const std::variant<Types...>& w );
(2) (since C++17)
template< class... Types >

constexpr bool operator<( const std::variant<Types...>& v,

                          const std::variant<Types...>& w );
(3) (since C++17)
template< class... Types >

constexpr bool operator>( const std::variant<Types...>& v,

                          const std::variant<Types...>& w );
(4) (since C++17)
template< class... Types >

constexpr bool operator<=( const std::variant<Types...>& v,

                           const std::variant<Types...>& w );
(5) (since C++17)
template< class... Types >

constexpr bool operator>=( const std::variant<Types...>& v,

                           const std::variant<Types...>& w );
(6) (since C++17)
template< class... Types >

constexpr std::common_comparison_category_t<
    std::compare_three_way_result_t<Types>...>
    operator<=>( const std::variant<Types...>& v,

                 const std::variant<Types...>& w );
(7) (since C++20)
1) Equality operator for variants:
  • If v.index() != w.index(), returns false;
  • otherwise if v.valueless_by_exception(), returns true;
  • otherwise returns std::get<v.index()>(v) == std::get<v.index()>(w). The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) == std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
2) Inequality operator for variants:
  • If v.index() != w.index(), returns true;
  • otherwise if v.valueless_by_exception(), returns false;
  • otherwise returns std::get<v.index()>(v) != std::get<v.index()>(w). The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) != std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
3) Less-than operator for variants:
  • If w.valueless_by_exception(), returns false;
  • otherwise if v.valueless_by_exception(), returns true;
  • otherwise if v.index() < w.index(), returns true;
  • otherwise if v.index() > w.index(), returns false;
  • otherwise returns std::get<v.index()>(v) < std::get<v.index()>(w). The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) < std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
4) Greater-than operator for variants:
  • If v.valueless_by_exception(), returns false;
  • otherwise if w.valueless_by_exception(), returns true;
  • otherwise if v.index() > w.index(), returns true;
  • otherwise if v.index() < w.index(), returns false;
  • otherwise returns std::get<v.index()>(v) > std::get<v.index()>(w). The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) > std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
5) Less-equal operator for variants:
  • If v.valueless_by_exception(), returns true;
  • otherwise if w.valueless_by_exception(), returns false;
  • otherwise if v.index() < w.index(), returns true;
  • otherwise if v.index() > w.index(), returns false;
  • otherwise returns std::get<v.index()>(v) <= std::get<v.index()>(w). The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) <= std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
6) Greater-equal operator for variants:
  • If w.valueless_by_exception(), returns true;
  • otherwise if v.valueless_by_exception(), returns false;
  • otherwise if v.index() > w.index(), returns true;
  • otherwise if v.index() < w.index(), returns false;
  • otherwise std::get<v.index()>(v) >= std::get<v.index()>(w).The behavior is undefined (until C++20)The program is ill-formed (since C++20) if std::get<i>(v) >= std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.
7) Three-way comparison operator for variants:
  • If both v.valueless_by_exception() and w.valueless_by_exception() are true, returns std::strong_ordering::equal;
  • otherwise if v.valueless_by_exception() is true, returns std::strong_ordering::less;
  • otherwise if w.valueless_by_exception() is true, returns std::strong_ordering::greater;
  • otherwise if v.index() != w.index(), returns v.index() <=> w.index();
  • otherwise equivalent to std::get<v.index()>(v) <=> std::get<v.index()>(w).

Contents

[edit] Parameters

v,w - variants to compare

[edit] Return value

The result of the comparison as described above.

[edit] Example

[edit] See also

(C++17)(C++17)(C++17)(C++17)(C++17)(C++17)(C++20)
compares optional objects
(function template) [edit]