value_rvalue.pass.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // XFAIL: dylib-has-no-bad_optional_access
  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(int, char**)
  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. (void)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. return 0;
  64. }