| 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 | |
| 23 | struct NonTDtor { |
| 24 | int* count; |
| 25 | constexpr NonTDtor(int* a, int*) : count(a) {} |
| 26 | TEST_CONSTEXPR_CXX20 ~NonTDtor() { ++*count; } |
| 27 | }; |
| 28 | static_assert(!std::is_trivially_destructible<NonTDtor>::value, "" ); |
| 29 | |
| 30 | struct NonTDtor1 { |
| 31 | int* count; |
| 32 | constexpr NonTDtor1(int*, int* b) : count(b) {} |
| 33 | TEST_CONSTEXPR_CXX20 ~NonTDtor1() { ++*count; } |
| 34 | }; |
| 35 | static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "" ); |
| 36 | |
| 37 | struct TDtor { |
| 38 | constexpr TDtor() = default; |
| 39 | constexpr TDtor(const TDtor&) {} // non-trivial copy |
| 40 | TEST_CONSTEXPR_CXX20 ~TDtor() = default; |
| 41 | }; |
| 42 | static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "" ); |
| 43 | static_assert(std::is_trivially_destructible<TDtor>::value, "" ); |
| 44 | |
| 45 | TEST_CONSTEXPR_CXX20 bool test() { |
| 46 | { |
| 47 | using V = std::variant<int, long, TDtor>; |
| 48 | static_assert(std::is_trivially_destructible<V>::value, "" ); |
| 49 | [[maybe_unused]] V v(std::in_place_index<2>); |
| 50 | } |
| 51 | { |
| 52 | using V = std::variant<NonTDtor, int, NonTDtor1>; |
| 53 | static_assert(!std::is_trivially_destructible<V>::value, "" ); |
| 54 | { |
| 55 | int count0 = 0; |
| 56 | int count1 = 0; |
| 57 | { |
| 58 | V v(std::in_place_index<0>, &count0, &count1); |
| 59 | assert(count0 == 0); |
| 60 | assert(count1 == 0); |
| 61 | } |
| 62 | assert(count0 == 1); |
| 63 | assert(count1 == 0); |
| 64 | } |
| 65 | { |
| 66 | int count0 = 0; |
| 67 | int count1 = 0; |
| 68 | { V v(std::in_place_index<1>); } |
| 69 | assert(count0 == 0); |
| 70 | assert(count1 == 0); |
| 71 | } |
| 72 | { |
| 73 | int count0 = 0; |
| 74 | int count1 = 0; |
| 75 | { |
| 76 | V v(std::in_place_index<2>, &count0, &count1); |
| 77 | assert(count0 == 0); |
| 78 | assert(count1 == 0); |
| 79 | } |
| 80 | assert(count0 == 0); |
| 81 | assert(count1 == 1); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | int main(int, char**) { |
| 89 | test(); |
| 90 | |
| 91 | #if TEST_STD_VER >= 20 |
| 92 | static_assert(test()); |
| 93 | #endif |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |