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 // type_traits
10 
11 // is_trivially_destructible
12 
13 #include <type_traits>
14 #include "test_macros.h"
15 
16 template <class T>
test_is_trivially_destructible()17 void test_is_trivially_destructible()
18 {
19     static_assert( std::is_trivially_destructible<T>::value, "");
20     static_assert( std::is_trivially_destructible<const T>::value, "");
21     static_assert( std::is_trivially_destructible<volatile T>::value, "");
22     static_assert( std::is_trivially_destructible<const volatile T>::value, "");
23 #if TEST_STD_VER > 14
24     static_assert( std::is_trivially_destructible_v<T>, "");
25     static_assert( std::is_trivially_destructible_v<const T>, "");
26     static_assert( std::is_trivially_destructible_v<volatile T>, "");
27     static_assert( std::is_trivially_destructible_v<const volatile T>, "");
28 #endif
29 }
30 
31 template <class T>
test_is_not_trivially_destructible()32 void test_is_not_trivially_destructible()
33 {
34     static_assert(!std::is_trivially_destructible<T>::value, "");
35     static_assert(!std::is_trivially_destructible<const T>::value, "");
36     static_assert(!std::is_trivially_destructible<volatile T>::value, "");
37     static_assert(!std::is_trivially_destructible<const volatile T>::value, "");
38 #if TEST_STD_VER > 14
39     static_assert(!std::is_trivially_destructible_v<T>, "");
40     static_assert(!std::is_trivially_destructible_v<const T>, "");
41     static_assert(!std::is_trivially_destructible_v<volatile T>, "");
42     static_assert(!std::is_trivially_destructible_v<const volatile T>, "");
43 #endif
44 }
45 
~PublicDestructorPublicDestructor46 struct PublicDestructor           { public:     ~PublicDestructor() {}};
~ProtectedDestructorProtectedDestructor47 struct ProtectedDestructor        { protected:  ~ProtectedDestructor() {}};
~PrivateDestructorPrivateDestructor48 struct PrivateDestructor          { private:    ~PrivateDestructor() {}};
49 
~VirtualPublicDestructorVirtualPublicDestructor50 struct VirtualPublicDestructor           { public:    virtual ~VirtualPublicDestructor() {}};
~VirtualProtectedDestructorVirtualProtectedDestructor51 struct VirtualProtectedDestructor        { protected: virtual ~VirtualProtectedDestructor() {}};
~VirtualPrivateDestructorVirtualPrivateDestructor52 struct VirtualPrivateDestructor          { private:   virtual ~VirtualPrivateDestructor() {}};
53 
54 struct PurePublicDestructor              { public:    virtual ~PurePublicDestructor() = 0; };
55 struct PureProtectedDestructor           { protected: virtual ~PureProtectedDestructor() = 0; };
56 struct PurePrivateDestructor             { private:   virtual ~PurePrivateDestructor() = 0; };
57 
58 
59 class Empty
60 {
61 };
62 
63 union Union {};
64 
65 struct bit_zero
66 {
67     int :  0;
68 };
69 
70 class Abstract
71 {
72     virtual void foo() = 0;
73 };
74 
75 class AbstractDestructor
76 {
77     virtual ~AbstractDestructor() = 0;
78 };
79 
80 struct A
81 {
82     ~A();
83 };
84 
main(int,char **)85 int main(int, char**)
86 {
87     test_is_not_trivially_destructible<void>();
88     test_is_not_trivially_destructible<A>();
89     test_is_not_trivially_destructible<char[]>();
90     test_is_not_trivially_destructible<VirtualPublicDestructor>();
91     test_is_not_trivially_destructible<PurePublicDestructor>();
92 
93     test_is_trivially_destructible<Abstract>();
94     test_is_trivially_destructible<Union>();
95     test_is_trivially_destructible<Empty>();
96     test_is_trivially_destructible<int&>();
97     test_is_trivially_destructible<int>();
98     test_is_trivially_destructible<double>();
99     test_is_trivially_destructible<int*>();
100     test_is_trivially_destructible<const int*>();
101     test_is_trivially_destructible<char[3]>();
102     test_is_trivially_destructible<bit_zero>();
103 
104 #if TEST_STD_VER >= 11
105     // requires access control sfinae
106     test_is_not_trivially_destructible<ProtectedDestructor>();
107     test_is_not_trivially_destructible<PrivateDestructor>();
108     test_is_not_trivially_destructible<VirtualProtectedDestructor>();
109     test_is_not_trivially_destructible<VirtualPrivateDestructor>();
110     test_is_not_trivially_destructible<PureProtectedDestructor>();
111     test_is_not_trivially_destructible<PurePrivateDestructor>();
112 #endif
113 
114 #if TEST_HAS_BUILTIN_IDENTIFIER(_Atomic)
115     test_is_trivially_destructible<_Atomic int>();
116     test_is_trivially_destructible<_Atomic float>();
117     test_is_trivially_destructible<_Atomic int*>();
118 #endif
119 
120   return 0;
121 }
122