dereference_rvalue.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // constexpr T&& optional<T>::operator*() &&;
  11. #ifdef _LIBCPP_DEBUG
  12. #define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
  13. #endif
  14. #include <optional>
  15. #include <type_traits>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. using std::optional;
  19. struct X
  20. {
  21. constexpr int test() const& {return 3;}
  22. int test() & {return 4;}
  23. constexpr int test() const&& {return 5;}
  24. int test() && {return 6;}
  25. };
  26. struct Y
  27. {
  28. constexpr int test() && {return 7;}
  29. };
  30. constexpr int
  31. test()
  32. {
  33. optional<Y> opt{Y{}};
  34. return (*std::move(opt)).test();
  35. }
  36. int main(int, char**)
  37. {
  38. {
  39. optional<X> opt; ((void)opt);
  40. ASSERT_SAME_TYPE(decltype(*std::move(opt)), X&&);
  41. // ASSERT_NOT_NOEXCEPT(*std::move(opt));
  42. // FIXME: This assertion fails with GCC because it can see that
  43. // (A) operator*() is constexpr, and
  44. // (B) there is no path through the function that throws.
  45. // It's arguable if this is the correct behavior for the noexcept
  46. // operator.
  47. // Regardless this function should still be noexcept(false) because
  48. // it has a narrow contract.
  49. }
  50. {
  51. optional<X> opt(X{});
  52. assert((*std::move(opt)).test() == 6);
  53. }
  54. static_assert(test() == 7, "");
  55. #ifdef _LIBCPP_DEBUG
  56. {
  57. optional<X> opt;
  58. assert((*std::move(opt)).test() == 3);
  59. assert(false);
  60. }
  61. #endif // _LIBCPP_DEBUG
  62. return 0;
  63. }