op_arrow.pass.cpp 1.7 KB

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