value_or.pass.cpp 1.4 KB

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