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