dereference.pass.cpp 1.7 KB

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