• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12 
13 // <variant>
14 
15 // template <class ...Types> class variant;
16 
17 // ~variant();
18 
19 #include <cassert>
20 #include <type_traits>
21 #include <variant>
22 
23 #include "test_macros.h"
24 
25 struct NonTDtor {
26   static int count;
27   NonTDtor() = default;
~NonTDtorNonTDtor28   ~NonTDtor() { ++count; }
29 };
30 int NonTDtor::count = 0;
31 static_assert(!std::is_trivially_destructible<NonTDtor>::value, "");
32 
33 struct NonTDtor1 {
34   static int count;
35   NonTDtor1() = default;
~NonTDtor1NonTDtor136   ~NonTDtor1() { ++count; }
37 };
38 int NonTDtor1::count = 0;
39 static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "");
40 
41 struct TDtor {
TDtorTDtor42   TDtor(const TDtor &) {} // non-trivial copy
43   ~TDtor() = default;
44 };
45 static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "");
46 static_assert(std::is_trivially_destructible<TDtor>::value, "");
47 
main()48 int main() {
49   {
50     using V = std::variant<int, long, TDtor>;
51     static_assert(std::is_trivially_destructible<V>::value, "");
52   }
53   {
54     using V = std::variant<NonTDtor, int, NonTDtor1>;
55     static_assert(!std::is_trivially_destructible<V>::value, "");
56     {
57       V v(std::in_place_index<0>);
58       assert(NonTDtor::count == 0);
59       assert(NonTDtor1::count == 0);
60     }
61     assert(NonTDtor::count == 1);
62     assert(NonTDtor1::count == 0);
63     NonTDtor::count = 0;
64     { V v(std::in_place_index<1>); }
65     assert(NonTDtor::count == 0);
66     assert(NonTDtor1::count == 0);
67     {
68       V v(std::in_place_index<2>);
69       assert(NonTDtor::count == 0);
70       assert(NonTDtor1::count == 0);
71     }
72     assert(NonTDtor::count == 0);
73     assert(NonTDtor1::count == 1);
74   }
75 }
76