return _VSTD::get_if<__find_exactly_one_t<_Tp, _Types...>::value>(__v);
}
+template <class _Operator>
+struct __convert_to_bool {
+ template <class _T1, class _T2>
+ _LIBCPP_INLINE_VISIBILITY constexpr bool operator()(_T1 && __t1, _T2&& __t2) const {
+ static_assert(std::is_convertible<decltype(_Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2))), bool>::value,
+ "the relational operator does not return a type which is implicitly convertible to bool");
+ return _Operator{}(_VSTD::forward<_T1>(__t1), _VSTD::forward<_T2>(__t2));
+ }
+};
+
template <class... _Types>
inline _LIBCPP_INLINE_VISIBILITY
constexpr bool operator==(const variant<_Types...>& __lhs,
using __variant_detail::__visitation::__variant;
if (__lhs.index() != __rhs.index()) return false;
if (__lhs.valueless_by_exception()) return true;
- return __variant::__visit_value_at(__lhs.index(), equal_to<>{}, __lhs, __rhs);
+ return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<equal_to<>>{}, __lhs, __rhs);
}
template <class... _Types>
if (__lhs.index() != __rhs.index()) return true;
if (__lhs.valueless_by_exception()) return false;
return __variant::__visit_value_at(
- __lhs.index(), not_equal_to<>{}, __lhs, __rhs);
+ __lhs.index(), __convert_to_bool<not_equal_to<>>{}, __lhs, __rhs);
}
template <class... _Types>
if (__lhs.valueless_by_exception()) return true;
if (__lhs.index() < __rhs.index()) return true;
if (__lhs.index() > __rhs.index()) return false;
- return __variant::__visit_value_at(__lhs.index(), less<>{}, __lhs, __rhs);
+ return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<less<>>{}, __lhs, __rhs);
}
template <class... _Types>
if (__rhs.valueless_by_exception()) return true;
if (__lhs.index() > __rhs.index()) return true;
if (__lhs.index() < __rhs.index()) return false;
- return __variant::__visit_value_at(__lhs.index(), greater<>{}, __lhs, __rhs);
+ return __variant::__visit_value_at(__lhs.index(), __convert_to_bool<greater<>>{}, __lhs, __rhs);
}
template <class... _Types>
if (__lhs.index() < __rhs.index()) return true;
if (__lhs.index() > __rhs.index()) return false;
return __variant::__visit_value_at(
- __lhs.index(), less_equal<>{}, __lhs, __rhs);
+ __lhs.index(), __convert_to_bool<less_equal<>>{}, __lhs, __rhs);
}
template <class... _Types>
if (__lhs.index() > __rhs.index()) return true;
if (__lhs.index() < __rhs.index()) return false;
return __variant::__visit_value_at(
- __lhs.index(), greater_equal<>{}, __lhs, __rhs);
+ __lhs.index(), __convert_to_bool<greater_equal<>>{}, __lhs, __rhs);
}
template <class _Visitor, class... _Vs>
}
#endif // TEST_HAS_NO_EXCEPTIONS
-void test_equality() {
+struct MyBool {
+ bool value;
+ constexpr explicit MyBool(bool v) : value(v) {}
+ constexpr operator bool() const noexcept { return value; }
+};
+
+struct ComparesToMyBool {
+ int value = 0;
+};
+inline constexpr MyBool operator==(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value == RHS.value);
+}
+inline constexpr MyBool operator!=(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value != RHS.value);
+}
+inline constexpr MyBool operator<(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value < RHS.value);
+}
+inline constexpr MyBool operator<=(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value <= RHS.value);
+}
+inline constexpr MyBool operator>(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value > RHS.value);
+}
+inline constexpr MyBool operator>=(const ComparesToMyBool& LHS, const ComparesToMyBool& RHS) noexcept {
+ return MyBool(LHS.value >= RHS.value);
+}
+
+template <class T1, class T2>
+void test_equality_basic() {
{
- using V = std::variant<int, long>;
- constexpr V v1(42);
- constexpr V v2(42);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{42});
+ constexpr V v2(std::in_place_index<0>, T1{42});
static_assert(v1 == v2, "");
static_assert(v2 == v1, "");
static_assert(!(v1 != v2), "");
static_assert(!(v2 != v1), "");
}
{
- using V = std::variant<int, long>;
- constexpr V v1(42);
- constexpr V v2(43);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{42});
+ constexpr V v2(std::in_place_index<0>, T1{43});
static_assert(!(v1 == v2), "");
static_assert(!(v2 == v1), "");
static_assert(v1 != v2, "");
static_assert(v2 != v1, "");
}
{
- using V = std::variant<int, long>;
- constexpr V v1(42);
- constexpr V v2(42l);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{42});
+ constexpr V v2(std::in_place_index<1>, T2{42});
static_assert(!(v1 == v2), "");
static_assert(!(v2 == v1), "");
static_assert(v1 != v2, "");
static_assert(v2 != v1, "");
}
{
- using V = std::variant<int, long>;
- constexpr V v1(42l);
- constexpr V v2(42l);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<1>, T2{42});
+ constexpr V v2(std::in_place_index<1>, T2{42});
static_assert(v1 == v2, "");
static_assert(v2 == v1, "");
static_assert(!(v1 != v2), "");
static_assert(!(v2 != v1), "");
}
+}
+
+void test_equality() {
+ test_equality_basic<int, long>();
+ test_equality_basic<ComparesToMyBool, int>();
+ test_equality_basic<int, ComparesToMyBool>();
+ test_equality_basic<ComparesToMyBool, ComparesToMyBool>();
#ifndef TEST_HAS_NO_EXCEPTIONS
{
using V = std::variant<int, MakeEmptyT>;
template <class Var>
constexpr bool test_less(const Var &l, const Var &r, bool expect_less,
bool expect_greater) {
+ static_assert(std::is_same_v<decltype(l < r), bool>, "");
+ static_assert(std::is_same_v<decltype(l <= r), bool>, "");
+ static_assert(std::is_same_v<decltype(l > r), bool>, "");
+ static_assert(std::is_same_v<decltype(l >= r), bool>, "");
+
return ((l < r) == expect_less) && (!(l >= r) == expect_less) &&
((l > r) == expect_greater) && (!(l <= r) == expect_greater);
}
-void test_relational() {
+template <class T1, class T2>
+void test_relational_basic() {
{ // same index, same value
- using V = std::variant<int, long>;
- constexpr V v1(1);
- constexpr V v2(1);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{1});
+ constexpr V v2(std::in_place_index<0>, T1{1});
static_assert(test_less(v1, v2, false, false), "");
}
{ // same index, value < other_value
- using V = std::variant<int, long>;
- constexpr V v1(0);
- constexpr V v2(1);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{0});
+ constexpr V v2(std::in_place_index<0>, T1{1});
static_assert(test_less(v1, v2, true, false), "");
}
{ // same index, value > other_value
- using V = std::variant<int, long>;
- constexpr V v1(1);
- constexpr V v2(0);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{1});
+ constexpr V v2(std::in_place_index<0>, T1{0});
static_assert(test_less(v1, v2, false, true), "");
}
{ // LHS.index() < RHS.index()
- using V = std::variant<int, long>;
- constexpr V v1(0);
- constexpr V v2(0l);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<0>, T1{0});
+ constexpr V v2(std::in_place_index<1>, T2{0});
static_assert(test_less(v1, v2, true, false), "");
}
{ // LHS.index() > RHS.index()
- using V = std::variant<int, long>;
- constexpr V v1(0l);
- constexpr V v2(0);
+ using V = std::variant<T1, T2>;
+ constexpr V v1(std::in_place_index<1>, T2{0});
+ constexpr V v2(std::in_place_index<0>, T1{0});
static_assert(test_less(v1, v2, false, true), "");
}
+}
+
+void test_relational() {
+ test_relational_basic<int, long>();
+ test_relational_basic<ComparesToMyBool, int>();
+ test_relational_basic<int, ComparesToMyBool>();
+ test_relational_basic<ComparesToMyBool, ComparesToMyBool>();
#ifndef TEST_HAS_NO_EXCEPTIONS
{ // LHS.index() < RHS.index(), RHS is empty
using V = std::variant<int, MakeEmptyT>;
--- /dev/null
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <variant>
+
+// template <class ...Types>
+// constexpr bool
+// operator==(variant<Types...> const&, variant<Types...> const&) noexcept;
+//
+// template <class ...Types>
+// constexpr bool
+// operator!=(variant<Types...> const&, variant<Types...> const&) noexcept;
+//
+// template <class ...Types>
+// constexpr bool
+// operator<(variant<Types...> const&, variant<Types...> const&) noexcept;
+//
+// template <class ...Types>
+// constexpr bool
+// operator>(variant<Types...> const&, variant<Types...> const&) noexcept;
+//
+// template <class ...Types>
+// constexpr bool
+// operator<=(variant<Types...> const&, variant<Types...> const&) noexcept;
+//
+// template <class ...Types>
+// constexpr bool
+// operator>=(variant<Types...> const&, variant<Types...> const&) noexcept;
+
+#include <cassert>
+#include <type_traits>
+#include <utility>
+#include <variant>
+
+#include "test_macros.h"
+
+
+struct MyBoolExplicit {
+ bool value;
+ constexpr explicit MyBoolExplicit(bool v) : value(v) {}
+ constexpr explicit operator bool() const noexcept { return value; }
+};
+
+struct ComparesToMyBoolExplicit {
+ int value = 0;
+};
+inline constexpr MyBoolExplicit operator==(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value == RHS.value);
+}
+inline constexpr MyBoolExplicit operator!=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value != RHS.value);
+}
+inline constexpr MyBoolExplicit operator<(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value < RHS.value);
+}
+inline constexpr MyBoolExplicit operator<=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value <= RHS.value);
+}
+inline constexpr MyBoolExplicit operator>(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value > RHS.value);
+}
+inline constexpr MyBoolExplicit operator>=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
+ return MyBoolExplicit(LHS.value >= RHS.value);
+}
+
+
+int main() {
+ using V = std::variant<int, ComparesToMyBoolExplicit>;
+ V v1(42);
+ V v2(101);
+ // expected-error-re@variant:* 6 {{static_assert failed due to requirement 'std::is_convertible{{[^']+}}' "the relational operator does not return a type which is implicitly convertible to bool"}}
+ // expected-error@variant:* 6 {{no viable conversion}}
+ (void)(v1 == v2); // expected-note {{here}}
+ (void)(v1 != v2); // expected-note {{here}}
+ (void)(v1 < v2); // expected-note {{here}}
+ (void)(v1 <= v2); // expected-note {{here}}
+ (void)(v1 > v2); // expected-note {{here}}
+ (void)(v1 >= v2); // expected-note {{here}}
+}