less_than.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) { return lhs.i_ < rhs.i_; }
  20. int main(int, char**) {
  21. {
  22. typedef X T;
  23. typedef optional<T> O;
  24. constexpr T val(2);
  25. constexpr O o1; // disengaged
  26. constexpr O o2{1}; // engaged
  27. constexpr O o3{val}; // engaged
  28. static_assert((o1 < T(1)), "");
  29. static_assert(!(o2 < T(1)), ""); // equal
  30. static_assert(!(o3 < T(1)), "");
  31. static_assert((o2 < val), "");
  32. static_assert(!(o3 < val), ""); // equal
  33. static_assert((o3 < T(3)), "");
  34. static_assert(!(T(1) < o1), "");
  35. static_assert(!(T(1) < o2), ""); // equal
  36. static_assert((T(1) < o3), "");
  37. static_assert(!(val < o2), "");
  38. static_assert(!(val < o3), ""); // equal
  39. static_assert(!(T(3) < o3), "");
  40. }
  41. {
  42. using O = optional<int>;
  43. constexpr O o1(42);
  44. static_assert(o1 < 101l, "");
  45. static_assert(!(42l < o1), "");
  46. }
  47. {
  48. using O = optional<const int>;
  49. constexpr O o1(42);
  50. static_assert(o1 < 101, "");
  51. static_assert(!(42 < o1), "");
  52. }
  53. return 0;
  54. }