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