value_const.pass.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // XFAIL: libcpp-no-exceptions
  10. // <optional>
  11. // constexpr const T& optional<T>::value() const;
  12. #include <experimental/optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #if _LIBCPP_STD_VER > 11
  16. using std::experimental::optional;
  17. using std::experimental::in_place_t;
  18. using std::experimental::in_place;
  19. using std::experimental::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. };
  27. #endif // _LIBCPP_STD_VER > 11
  28. int main()
  29. {
  30. #if _LIBCPP_STD_VER > 11
  31. {
  32. constexpr optional<X> opt(in_place);
  33. static_assert(opt.value().test() == 3, "");
  34. }
  35. {
  36. const optional<X> opt(in_place);
  37. assert(opt.value().test() == 3);
  38. }
  39. {
  40. const optional<X> opt;
  41. try
  42. {
  43. opt.value();
  44. assert(false);
  45. }
  46. catch (const bad_optional_access&)
  47. {
  48. }
  49. }
  50. #endif // _LIBCPP_STD_VER > 11
  51. }