nullopt_t.pass.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // optional<T>& operator=(nullopt_t) noexcept;
  12. #include <optional>
  13. #include <type_traits>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "archetypes.hpp"
  17. using std::optional;
  18. using std::nullopt_t;
  19. using std::nullopt;
  20. int main()
  21. {
  22. {
  23. optional<int> opt;
  24. static_assert(noexcept(opt = nullopt) == true, "");
  25. opt = nullopt;
  26. assert(static_cast<bool>(opt) == false);
  27. }
  28. {
  29. optional<int> opt(3);
  30. opt = nullopt;
  31. assert(static_cast<bool>(opt) == false);
  32. }
  33. using TT = TestTypes::TestType;
  34. TT::reset();
  35. {
  36. optional<TT> opt;
  37. static_assert(noexcept(opt = nullopt) == true, "");
  38. assert(TT::destroyed == 0);
  39. opt = nullopt;
  40. assert(TT::constructed == 0);
  41. assert(TT::alive == 0);
  42. assert(TT::destroyed == 0);
  43. assert(static_cast<bool>(opt) == false);
  44. }
  45. assert(TT::alive == 0);
  46. assert(TT::destroyed == 0);
  47. TT::reset();
  48. {
  49. optional<TT> opt(42);
  50. assert(TT::destroyed == 0);
  51. TT::reset_constructors();
  52. opt = nullopt;
  53. assert(TT::constructed == 0);
  54. assert(TT::alive == 0);
  55. assert(TT::destroyed == 1);
  56. assert(static_cast<bool>(opt) == false);
  57. }
  58. assert(TT::alive == 0);
  59. assert(TT::destroyed == 1);
  60. TT::reset();
  61. }