reset.pass.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. // void reset() noexcept;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. using std::optional;
  16. struct X
  17. {
  18. static bool dtor_called;
  19. ~X() {dtor_called = true;}
  20. };
  21. bool X::dtor_called = false;
  22. int main()
  23. {
  24. {
  25. optional<int> opt;
  26. static_assert(noexcept(opt.reset()) == true, "");
  27. opt.reset();
  28. assert(static_cast<bool>(opt) == false);
  29. }
  30. {
  31. optional<int> opt(3);
  32. opt.reset();
  33. assert(static_cast<bool>(opt) == false);
  34. }
  35. {
  36. optional<X> opt;
  37. static_assert(noexcept(opt.reset()) == true, "");
  38. assert(X::dtor_called == false);
  39. opt.reset();
  40. assert(X::dtor_called == false);
  41. assert(static_cast<bool>(opt) == false);
  42. }
  43. assert(X::dtor_called == false); // TRANSITION, Clang/C2 VSO#239997
  44. {
  45. optional<X> opt(X{});
  46. X::dtor_called = false;
  47. opt.reset();
  48. assert(X::dtor_called == true);
  49. assert(static_cast<bool>(opt) == false);
  50. X::dtor_called = false;
  51. }
  52. assert(X::dtor_called == false); // TRANSITION, Clang/C2 VSO#239997
  53. }