dtor.pass.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // UNSUPPORTED: c++98, c++03, c++11, c++14
  10. // <variant>
  11. // template <class ...Types> class variant;
  12. // ~variant();
  13. #include <cassert>
  14. #include <type_traits>
  15. #include <variant>
  16. #include "test_macros.h"
  17. struct NonTDtor {
  18. static int count;
  19. NonTDtor() = default;
  20. ~NonTDtor() { ++count; }
  21. };
  22. int NonTDtor::count = 0;
  23. static_assert(!std::is_trivially_destructible<NonTDtor>::value, "");
  24. struct NonTDtor1 {
  25. static int count;
  26. NonTDtor1() = default;
  27. ~NonTDtor1() { ++count; }
  28. };
  29. int NonTDtor1::count = 0;
  30. static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "");
  31. struct TDtor {
  32. TDtor(const TDtor &) {} // non-trivial copy
  33. ~TDtor() = default;
  34. };
  35. static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "");
  36. static_assert(std::is_trivially_destructible<TDtor>::value, "");
  37. int main(int, char**) {
  38. {
  39. using V = std::variant<int, long, TDtor>;
  40. static_assert(std::is_trivially_destructible<V>::value, "");
  41. }
  42. {
  43. using V = std::variant<NonTDtor, int, NonTDtor1>;
  44. static_assert(!std::is_trivially_destructible<V>::value, "");
  45. {
  46. V v(std::in_place_index<0>);
  47. assert(NonTDtor::count == 0);
  48. assert(NonTDtor1::count == 0);
  49. }
  50. assert(NonTDtor::count == 1);
  51. assert(NonTDtor1::count == 0);
  52. NonTDtor::count = 0;
  53. { V v(std::in_place_index<1>); }
  54. assert(NonTDtor::count == 0);
  55. assert(NonTDtor1::count == 0);
  56. {
  57. V v(std::in_place_index<2>);
  58. assert(NonTDtor::count == 0);
  59. assert(NonTDtor1::count == 0);
  60. }
  61. assert(NonTDtor::count == 0);
  62. assert(NonTDtor1::count == 1);
  63. }
  64. return 0;
  65. }