relops_bool_conv.fail.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <variant>
  11. // template <class ...Types>
  12. // constexpr bool
  13. // operator==(variant<Types...> const&, variant<Types...> const&) noexcept;
  14. //
  15. // template <class ...Types>
  16. // constexpr bool
  17. // operator!=(variant<Types...> const&, variant<Types...> const&) noexcept;
  18. //
  19. // template <class ...Types>
  20. // constexpr bool
  21. // operator<(variant<Types...> const&, variant<Types...> const&) noexcept;
  22. //
  23. // template <class ...Types>
  24. // constexpr bool
  25. // operator>(variant<Types...> const&, variant<Types...> const&) noexcept;
  26. //
  27. // template <class ...Types>
  28. // constexpr bool
  29. // operator<=(variant<Types...> const&, variant<Types...> const&) noexcept;
  30. //
  31. // template <class ...Types>
  32. // constexpr bool
  33. // operator>=(variant<Types...> const&, variant<Types...> const&) noexcept;
  34. #include <cassert>
  35. #include <type_traits>
  36. #include <utility>
  37. #include <variant>
  38. #include "test_macros.h"
  39. struct MyBoolExplicit {
  40. bool value;
  41. constexpr explicit MyBoolExplicit(bool v) : value(v) {}
  42. constexpr explicit operator bool() const noexcept { return value; }
  43. };
  44. struct ComparesToMyBoolExplicit {
  45. int value = 0;
  46. };
  47. inline constexpr MyBoolExplicit operator==(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  48. return MyBoolExplicit(LHS.value == RHS.value);
  49. }
  50. inline constexpr MyBoolExplicit operator!=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  51. return MyBoolExplicit(LHS.value != RHS.value);
  52. }
  53. inline constexpr MyBoolExplicit operator<(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  54. return MyBoolExplicit(LHS.value < RHS.value);
  55. }
  56. inline constexpr MyBoolExplicit operator<=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  57. return MyBoolExplicit(LHS.value <= RHS.value);
  58. }
  59. inline constexpr MyBoolExplicit operator>(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  60. return MyBoolExplicit(LHS.value > RHS.value);
  61. }
  62. inline constexpr MyBoolExplicit operator>=(const ComparesToMyBoolExplicit& LHS, const ComparesToMyBoolExplicit& RHS) noexcept {
  63. return MyBoolExplicit(LHS.value >= RHS.value);
  64. }
  65. int main(int, char**) {
  66. using V = std::variant<int, ComparesToMyBoolExplicit>;
  67. V v1(42);
  68. V v2(101);
  69. // expected-error-re@variant:* 6 {{static_assert failed {{.*}}"the relational operator does not return a type which is implicitly convertible to bool"}}
  70. // expected-error@variant:* 6 {{no viable conversion}}
  71. (void)(v1 == v2); // expected-note {{here}}
  72. (void)(v1 != v2); // expected-note {{here}}
  73. (void)(v1 < v2); // expected-note {{here}}
  74. (void)(v1 <= v2); // expected-note {{here}}
  75. (void)(v1 > v2); // expected-note {{here}}
  76. (void)(v1 >= v2); // expected-note {{here}}
  77. return 0;
  78. }