value_rvalue.pass.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // constexpr T& optional<T>::value() &&;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. using std::optional;
  17. using std::bad_optional_access;
  18. struct X
  19. {
  20. X() = default;
  21. X(const X&) = delete;
  22. constexpr int test() const & {return 3;}
  23. int test() & {return 4;}
  24. constexpr int test() const && {return 5;}
  25. int test() && {return 6;}
  26. };
  27. struct Y
  28. {
  29. constexpr int test() && {return 7;}
  30. };
  31. constexpr int
  32. test()
  33. {
  34. optional<Y> opt{Y{}};
  35. return std::move(opt).value().test();
  36. }
  37. int main()
  38. {
  39. {
  40. optional<X> opt; ((void)opt);
  41. ASSERT_NOT_NOEXCEPT(std::move(opt).value());
  42. ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X&&);
  43. }
  44. {
  45. optional<X> opt;
  46. opt.emplace();
  47. assert(std::move(opt).value().test() == 6);
  48. }
  49. #ifndef TEST_HAS_NO_EXCEPTIONS
  50. {
  51. optional<X> opt;
  52. try
  53. {
  54. std::move(opt).value();
  55. assert(false);
  56. }
  57. catch (const bad_optional_access&)
  58. {
  59. }
  60. }
  61. #endif
  62. static_assert(test() == 7, "");
  63. }