dereference_const_rvalue.pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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*() const &&;
  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. int test() const && {return 2;}
  29. };
  30. int main(int, char**)
  31. {
  32. {
  33. const optional<X> opt; ((void)opt);
  34. ASSERT_SAME_TYPE(decltype(*std::move(opt)), X const &&);
  35. // ASSERT_NOT_NOEXCEPT(*std::move(opt));
  36. // FIXME: This assertion fails with GCC because it can see that
  37. // (A) operator*() is constexpr, and
  38. // (B) there is no path through the function that throws.
  39. // It's arguable if this is the correct behavior for the noexcept
  40. // operator.
  41. // Regardless this function should still be noexcept(false) because
  42. // it has a narrow contract.
  43. }
  44. {
  45. constexpr optional<X> opt(X{});
  46. static_assert((*std::move(opt)).test() == 5, "");
  47. }
  48. {
  49. constexpr optional<Y> opt(Y{});
  50. assert((*std::move(opt)).test() == 2);
  51. }
  52. #ifdef _LIBCPP_DEBUG
  53. {
  54. optional<X> opt;
  55. assert((*std::move(opt)).test() == 5);
  56. assert(false);
  57. }
  58. #endif // _LIBCPP_DEBUG
  59. return 0;
  60. }