value_or.pass.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  10. // <optional>
  11. // template <class U> T optional<T>::value_or(U&& v) &&;
  12. #include <experimental/optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. using std::experimental::optional;
  16. using std::experimental::in_place_t;
  17. using std::experimental::in_place;
  18. struct Y
  19. {
  20. int i_;
  21. Y(int i) : i_(i) {}
  22. };
  23. struct X
  24. {
  25. int i_;
  26. X(int i) : i_(i) {}
  27. X(X&& x) : i_(x.i_) {x.i_ = 0;}
  28. X(const Y& y) : i_(y.i_) {}
  29. X(Y&& y) : i_(y.i_+1) {}
  30. friend constexpr bool operator==(const X& x, const X& y)
  31. {return x.i_ == y.i_;}
  32. };
  33. int main()
  34. {
  35. {
  36. optional<X> opt(in_place, 2);
  37. Y y(3);
  38. assert(std::move(opt).value_or(y) == 2);
  39. assert(*opt == 0);
  40. }
  41. {
  42. optional<X> opt(in_place, 2);
  43. assert(std::move(opt).value_or(Y(3)) == 2);
  44. assert(*opt == 0);
  45. }
  46. {
  47. optional<X> opt;
  48. Y y(3);
  49. assert(std::move(opt).value_or(y) == 3);
  50. assert(!opt);
  51. }
  52. {
  53. optional<X> opt;
  54. assert(std::move(opt).value_or(Y(3)) == 4);
  55. assert(!opt);
  56. }
  57. }