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