op_arrow_const.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  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 <experimental/optional>
  16. #include <type_traits>
  17. #include <cassert>
  18. using std::experimental::optional;
  19. struct X
  20. {
  21. constexpr int test() const {return 3;}
  22. };
  23. struct Y
  24. {
  25. int test() const {return 2;}
  26. };
  27. struct Z
  28. {
  29. const Z* operator&() const {return this;}
  30. constexpr int test() const {return 1;}
  31. };
  32. int main()
  33. {
  34. {
  35. constexpr optional<X> opt(X{});
  36. static_assert(opt->test() == 3, "");
  37. }
  38. {
  39. constexpr optional<Y> opt(Y{});
  40. assert(opt->test() == 2);
  41. }
  42. {
  43. constexpr optional<Z> opt(Z{});
  44. assert(opt->test() == 1);
  45. #ifndef _LIBCPP_HAS_NO_BUILTIN_ADDRESSOF
  46. static_assert(opt->test() == 1, "");
  47. #endif
  48. }
  49. #ifdef _LIBCPP_DEBUG
  50. {
  51. const optional<X> opt;
  52. assert(opt->test() == 3);
  53. assert(false);
  54. }
  55. #endif // _LIBCPP_DEBUG
  56. }