1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // Throwing bad_variant_access is supported starting in macosx10.13
13 // XFAIL: with_system_cxx_lib=macosx10.12 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
16 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
17 
18 // <variant>
19 
20 // template <class ...Types>
21 // constexpr bool
22 // operator==(variant<Types...> const&, variant<Types...> const&) noexcept;
23 //
24 // template <class ...Types>
25 // constexpr bool
26 // operator!=(variant<Types...> const&, variant<Types...> const&) noexcept;
27 //
28 // template <class ...Types>
29 // constexpr bool
30 // operator<(variant<Types...> const&, variant<Types...> const&) noexcept;
31 //
32 // template <class ...Types>
33 // constexpr bool
34 // operator>(variant<Types...> const&, variant<Types...> const&) noexcept;
35 //
36 // template <class ...Types>
37 // constexpr bool
38 // operator<=(variant<Types...> const&, variant<Types...> const&) noexcept;
39 //
40 // template <class ...Types>
41 // constexpr bool
42 // operator>=(variant<Types...> const&, variant<Types...> const&) noexcept;
43 
44 #include <cassert>
45 #include <type_traits>
46 #include <utility>
47 #include <variant>
48 
49 #include "test_macros.h"
50 
51 
52 struct MyBoolExplicit {
53   bool value;
54   constexpr explicit MyBoolExplicit(bool v) : value(v) {}
55   constexpr explicit operator bool() const noexcept { return value; }
56 };
57 
58 struct ComparesToMyBoolExplicit {
59   int value = 0;
60 };
61 inline constexpr MyBoolExplicit operator==(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
62   return MyBoolExplicit(LHS.value == RHS.value);
63 }
64 inline constexpr MyBoolExplicit operator!=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
65   return MyBoolExplicit(LHS.value != RHS.value);
66 }
67 inline constexpr MyBoolExplicit operator<(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
68   return MyBoolExplicit(LHS.value < RHS.value);
69 }
70 inline constexpr MyBoolExplicit operator<=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
71   return MyBoolExplicit(LHS.value <= RHS.value);
72 }
73 inline constexpr MyBoolExplicit operator>(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
74   return MyBoolExplicit(LHS.value > RHS.value);
75 }
76 inline constexpr MyBoolExplicit operator>=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
77   return MyBoolExplicit(LHS.value >= RHS.value);
78 }
79 
80 
81 int main(int, char**) {
82   using V = std::variant<int, ComparesToMyBoolExplicit>;
83   V v1(42);
84   V v2(101);
85   // expected-error-re@variant:* 6 {{static_assert failed {{.*}}"the relational operator does not return a type which is implicitly convertible to bool"}}
86   // expected-error@variant:* 6 {{no viable conversion}}
87   (void)(v1 == v2); // expected-note {{here}}
88   (void)(v1 != v2); // expected-note {{here}}
89   (void)(v1 < v2); // expected-note {{here}}
90   (void)(v1 <= v2); // expected-note {{here}}
91   (void)(v1 > v2); // expected-note {{here}}
92   (void)(v1 >= v2); // expected-note {{here}}
93 
94   return 0;
95 }
96