nullopt_t.pass.cpp 1.5 KB

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