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

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)
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 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 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 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 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 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 if std::get<i>(v) >= std::get<i>(w) is not a valid expression returning a type convertible to bool, for any i.

Parameters

v,w - variants to compare

Return value

The boolean result of the comparison as described above.

Example

See also

(C++17)
compares optional objects
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/variant/operator_cmp