less_equal.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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)), ""); // equal
  32. static_assert(!(o3 <= T(1)), "");
  33. static_assert((o2 <= val), "");
  34. static_assert((o3 <= val), ""); // equal
  35. static_assert((o3 <= T(3)), "");
  36. static_assert(!(T(1) <= o1), "");
  37. static_assert((T(1) <= o2), ""); // equal
  38. static_assert((T(1) <= o3), "");
  39. static_assert(!(val <= o2), "");
  40. static_assert((val <= o3), ""); // equal
  41. static_assert(!(T(3) <= o3), "");
  42. }
  43. {
  44. using O = optional<int>;
  45. constexpr O o1(42);
  46. static_assert(o1 <= 42l, "");
  47. static_assert(!(101l <= o1), "");
  48. }
  49. {
  50. using O = optional<const int>;
  51. constexpr O o1(42);
  52. static_assert(o1 <= 42, "");
  53. static_assert(!(101 <= o1), "");
  54. }
  55. return 0;
  56. }