97 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 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 int* count;25 constexpr NonTDtor(int* a, int*) : count(a) {}26 TEST_CONSTEXPR_CXX20 ~NonTDtor() { ++*count; }27};28static_assert(!std::is_trivially_destructible<NonTDtor>::value, "");29 30struct NonTDtor1 {31 int* count;32 constexpr NonTDtor1(int*, int* b) : count(b) {}33 TEST_CONSTEXPR_CXX20 ~NonTDtor1() { ++*count; }34};35static_assert(!std::is_trivially_destructible<NonTDtor1>::value, "");36 37struct TDtor {38 constexpr TDtor() = default;39 constexpr TDtor(const TDtor&) {} // non-trivial copy40 TEST_CONSTEXPR_CXX20 ~TDtor() = default;41};42static_assert(!std::is_trivially_copy_constructible<TDtor>::value, "");43static_assert(std::is_trivially_destructible<TDtor>::value, "");44 45TEST_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 88int main(int, char**) {89 test();90 91#if TEST_STD_VER >= 2092 static_assert(test());93#endif94 95 return 0;96}97