dereference_const_rvalue.pass.cpp 1.8 KB

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