value_or_const.pass.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <optional>
  11. // template <class U> constexpr T optional<T>::value_or(U&& v) const&;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. using std::optional;
  16. struct Y
  17. {
  18. int i_;
  19. constexpr Y(int i) : i_(i) {}
  20. };
  21. struct X
  22. {
  23. int i_;
  24. constexpr X(int i) : i_(i) {}
  25. constexpr X(const Y& y) : i_(y.i_) {}
  26. constexpr X(Y&& y) : i_(y.i_+1) {}
  27. friend constexpr bool operator==(const X& x, const X& y)
  28. {return x.i_ == y.i_;}
  29. };
  30. int main()
  31. {
  32. {
  33. constexpr optional<X> opt(2);
  34. constexpr Y y(3);
  35. static_assert(opt.value_or(y) == 2, "");
  36. }
  37. {
  38. constexpr optional<X> opt(2);
  39. static_assert(opt.value_or(Y(3)) == 2, "");
  40. }
  41. {
  42. constexpr optional<X> opt;
  43. constexpr Y y(3);
  44. static_assert(opt.value_or(y) == 3, "");
  45. }
  46. {
  47. constexpr optional<X> opt;
  48. static_assert(opt.value_or(Y(3)) == 4, "");
  49. }
  50. {
  51. const optional<X> opt(2);
  52. const Y y(3);
  53. assert(opt.value_or(y) == 2);
  54. }
  55. {
  56. const optional<X> opt(2);
  57. assert(opt.value_or(Y(3)) == 2);
  58. }
  59. {
  60. const optional<X> opt;
  61. const Y y(3);
  62. assert(opt.value_or(y) == 3);
  63. }
  64. {
  65. const optional<X> opt;
  66. assert(opt.value_or(Y(3)) == 4);
  67. }
  68. }