op_arrow_const.pass.cpp 1.9 KB

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