dtor.pass.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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();
  11. #include <optional>
  12. #include <type_traits>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. using std::optional;
  16. struct PODType {
  17. int value;
  18. int value2;
  19. };
  20. class X
  21. {
  22. public:
  23. static bool dtor_called;
  24. X() = default;
  25. ~X() {dtor_called = true;}
  26. };
  27. bool X::dtor_called = false;
  28. int main(int, char**)
  29. {
  30. {
  31. typedef int T;
  32. static_assert(std::is_trivially_destructible<T>::value, "");
  33. static_assert(std::is_trivially_destructible<optional<T>>::value, "");
  34. static_assert(std::is_literal_type<optional<T>>::value, "");
  35. }
  36. {
  37. typedef double T;
  38. static_assert(std::is_trivially_destructible<T>::value, "");
  39. static_assert(std::is_trivially_destructible<optional<T>>::value, "");
  40. static_assert(std::is_literal_type<optional<T>>::value, "");
  41. }
  42. {
  43. typedef PODType T;
  44. static_assert(std::is_trivially_destructible<T>::value, "");
  45. static_assert(std::is_trivially_destructible<optional<T>>::value, "");
  46. static_assert(std::is_literal_type<optional<T>>::value, "");
  47. }
  48. {
  49. typedef X T;
  50. static_assert(!std::is_trivially_destructible<T>::value, "");
  51. static_assert(!std::is_trivially_destructible<optional<T>>::value, "");
  52. static_assert(!std::is_literal_type<optional<T>>::value, "");
  53. {
  54. X x;
  55. optional<X> opt{x};
  56. assert(X::dtor_called == false);
  57. }
  58. assert(X::dtor_called == true);
  59. }
  60. return 0;
  61. }