value_const_rvalue.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 const T& optional<T>::value() const &&;
  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. using std::bad_optional_access;
  20. struct X
  21. {
  22. X() = default;
  23. X(const X&) = delete;
  24. constexpr int test() const & {return 3;}
  25. int test() & {return 4;}
  26. constexpr int test() const && {return 5;}
  27. int test() && {return 6;}
  28. };
  29. int main()
  30. {
  31. {
  32. const optional<X> opt; ((void)opt);
  33. ASSERT_NOT_NOEXCEPT(std::move(opt).value());
  34. ASSERT_SAME_TYPE(decltype(std::move(opt).value()), X const&&);
  35. }
  36. {
  37. constexpr optional<X> opt(in_place);
  38. static_assert(std::move(opt).value().test() == 5, "");
  39. }
  40. {
  41. const optional<X> opt(in_place);
  42. assert(std::move(opt).value().test() == 5);
  43. }
  44. #ifndef TEST_HAS_NO_EXCEPTIONS
  45. {
  46. const optional<X> opt;
  47. try
  48. {
  49. std::move(opt).value();
  50. assert(false);
  51. }
  52. catch (const bad_optional_access&)
  53. {
  54. }
  55. }
  56. #endif
  57. }