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

source code of libcxx/test/std/utilities/variant/variant.variant/variant.dtor/dtor.pass.cpp