reset.pass.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // <optional>
  10. // void reset() noexcept;
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include "test_macros.h"
  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(int, char**)
  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. {
  44. optional<X> opt(X{});
  45. X::dtor_called = false;
  46. opt.reset();
  47. assert(X::dtor_called == true);
  48. assert(static_cast<bool>(opt) == false);
  49. X::dtor_called = false;
  50. }
  51. return 0;
  52. }