op_arrow_const.pass.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 const 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. };
  24. struct Y
  25. {
  26. int test() const noexcept {return 2;}
  27. };
  28. struct Z
  29. {
  30. const Z* operator&() const;
  31. constexpr int test() const {return 1;}
  32. };
  33. int main()
  34. {
  35. {
  36. const std::optional<X> opt; ((void)opt);
  37. ASSERT_SAME_TYPE(decltype(opt.operator->()), X const*);
  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. constexpr optional<X> opt(X{});
  49. static_assert(opt->test() == 3, "");
  50. }
  51. {
  52. constexpr optional<Y> opt(Y{});
  53. assert(opt->test() == 2);
  54. }
  55. {
  56. constexpr optional<Z> opt(Z{});
  57. static_assert(opt->test() == 1, "");
  58. }
  59. #ifdef _LIBCPP_DEBUG
  60. {
  61. const optional<X> opt;
  62. assert(opt->test() == 3);
  63. assert(false);
  64. }
  65. #endif // _LIBCPP_DEBUG
  66. }