op_arrow.pass.cpp 1.6 KB

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