equal.pass.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <optional>
  10. // template <class T, class U> constexpr bool operator==(const optional<T>& x, const U& v);
  11. // template <class T, class U> constexpr bool operator==(const U& v, const optional<T>& x);
  12. #include <optional>
  13. #include "test_macros.h"
  14. using std::optional;
  15. struct X {
  16. int i_;
  17. constexpr X(int i) : i_(i) {}
  18. };
  19. constexpr bool operator==(const X& lhs, const X& rhs) {
  20. return lhs.i_ == rhs.i_;
  21. }
  22. int main(int, char**) {
  23. {
  24. typedef X T;
  25. typedef optional<T> O;
  26. constexpr T val(2);
  27. constexpr O o1; // disengaged
  28. constexpr O o2{1}; // engaged
  29. constexpr O o3{val}; // engaged
  30. static_assert(!(o1 == T(1)), "");
  31. static_assert((o2 == T(1)), "");
  32. static_assert(!(o3 == T(1)), "");
  33. static_assert((o3 == T(2)), "");
  34. static_assert((o3 == val), "");
  35. static_assert(!(T(1) == o1), "");
  36. static_assert((T(1) == o2), "");
  37. static_assert(!(T(1) == o3), "");
  38. static_assert((T(2) == o3), "");
  39. static_assert((val == o3), "");
  40. }
  41. {
  42. using O = optional<int>;
  43. constexpr O o1(42);
  44. static_assert(o1 == 42l, "");
  45. static_assert(!(101l == o1), "");
  46. }
  47. {
  48. using O = optional<const int>;
  49. constexpr O o1(42);
  50. static_assert(o1 == 42, "");
  51. static_assert(!(101 == o1), "");
  52. }
  53. return 0;
  54. }