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 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // Throwing bad_variant_access is supported starting in macosx10.13
13 // XFAIL: with_system_cxx_lib=macosx10.12 && !no-exceptions
14 // XFAIL: with_system_cxx_lib=macosx10.11 && !no-exceptions
15 // XFAIL: with_system_cxx_lib=macosx10.10 && !no-exceptions
16 // XFAIL: with_system_cxx_lib=macosx10.9 && !no-exceptions
17 
18 // <variant>
19 
20 // template <class ...Types> class variant;
21 
22 // ~variant();
23 
24 #include <cassert>
25 #include <type_traits>
26 #include <variant>
27 
28 #include "test_macros.h"
29 
30 struct NonTDtor {
31   static int count;
32   NonTDtor() = default;
33   ~NonTDtor() { ++count; }
34 };
35 int NonTDtor::count = 0;
36 static_assert(!std::is_trivially_destructible<NonTDtor>::value, "");
37 
38 struct NonTDtor1 {
39   static int count;
40   NonTDtor1() = default;
41   ~NonTDtor1() { ++count; }
42 };
43 int NonTDtor1::count = 0;
44 static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "");
45 
46 struct TDtor {
47   TDtor(const TDtor &) {} // non-trivial copy
48   ~TDtor() = default;
49 };
50 static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "");
51 static_assert(std::is_trivially_destructible<TDtor>::value, "");
52 
53 int main(int, char**) {
54   {
55     using V = std::variant<int, long, TDtor>;
56     static_assert(std::is_trivially_destructible<V>::value, "");
57   }
58   {
59     using V = std::variant<NonTDtor, int, NonTDtor1>;
60     static_assert(!std::is_trivially_destructible<V>::value, "");
61     {
62       V v(std::in_place_index<0>);
63       assert(NonTDtor::count == 0);
64       assert(NonTDtor1::count == 0);
65     }
66     assert(NonTDtor::count == 1);
67     assert(NonTDtor1::count == 0);
68     NonTDtor::count = 0;
69     { V v(std::in_place_index<1>); }
70     assert(NonTDtor::count == 0);
71     assert(NonTDtor1::count == 0);
72     {
73       V v(std::in_place_index<2>);
74       assert(NonTDtor::count == 0);
75       assert(NonTDtor1::count == 0);
76     }
77     assert(NonTDtor::count == 0);
78     assert(NonTDtor1::count == 1);
79   }
80 
81   return 0;
82 }
83