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