value_const.pass.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // XFAIL: dylib-has-no-bad_optional_access
  10. // <optional>
  11. // constexpr const T& optional<T>::value() const &;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. using std::optional;
  17. using std::in_place_t;
  18. using std::in_place;
  19. using std::bad_optional_access;
  20. struct X
  21. {
  22. X() = default;
  23. X(const X&) = delete;
  24. constexpr int test() const & {return 3;}
  25. int test() & {return 4;}
  26. constexpr int test() const && {return 5;}
  27. int test() && {return 6;}
  28. };
  29. int main(int, char**)
  30. {
  31. {
  32. const optional<X> opt; ((void)opt);
  33. ASSERT_NOT_NOEXCEPT(opt.value());
  34. ASSERT_SAME_TYPE(decltype(opt.value()), X const&);
  35. }
  36. {
  37. constexpr optional<X> opt(in_place);
  38. static_assert(opt.value().test() == 3, "");
  39. }
  40. {
  41. const optional<X> opt(in_place);
  42. assert(opt.value().test() == 3);
  43. }
  44. #ifndef TEST_HAS_NO_EXCEPTIONS
  45. {
  46. const optional<X> opt;
  47. try
  48. {
  49. (void)opt.value();
  50. assert(false);
  51. }
  52. catch (const bad_optional_access&)
  53. {
  54. }
  55. }
  56. #endif
  57. return 0;
  58. }