1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // type_traits
11
12 // is_trivially_destructible
13
14 // Prevent warning when testing the Abstract test type.
15 #if defined(__clang__)
16 #pragma clang diagnostic ignored "-Wdelete-non-virtual-dtor"
17 #endif
18
19 #include <type_traits>
20 #include "test_macros.h"
21
22 template <class T>
test_is_trivially_destructible()23 void test_is_trivially_destructible()
24 {
25 static_assert( std::is_trivially_destructible<T>::value, "");
26 static_assert( std::is_trivially_destructible<const T>::value, "");
27 static_assert( std::is_trivially_destructible<volatile T>::value, "");
28 static_assert( std::is_trivially_destructible<const volatile T>::value, "");
29 #if TEST_STD_VER > 14
30 static_assert( std::is_trivially_destructible_v<T>, "");
31 static_assert( std::is_trivially_destructible_v<const T>, "");
32 static_assert( std::is_trivially_destructible_v<volatile T>, "");
33 static_assert( std::is_trivially_destructible_v<const volatile T>, "");
34 #endif
35 }
36
37 template <class T>
test_is_not_trivially_destructible()38 void test_is_not_trivially_destructible()
39 {
40 static_assert(!std::is_trivially_destructible<T>::value, "");
41 static_assert(!std::is_trivially_destructible<const T>::value, "");
42 static_assert(!std::is_trivially_destructible<volatile T>::value, "");
43 static_assert(!std::is_trivially_destructible<const volatile T>::value, "");
44 #if TEST_STD_VER > 14
45 static_assert(!std::is_trivially_destructible_v<T>, "");
46 static_assert(!std::is_trivially_destructible_v<const T>, "");
47 static_assert(!std::is_trivially_destructible_v<volatile T>, "");
48 static_assert(!std::is_trivially_destructible_v<const volatile T>, "");
49 #endif
50 }
51
~PublicDestructorPublicDestructor52 struct PublicDestructor { public: ~PublicDestructor() {}};
~ProtectedDestructorProtectedDestructor53 struct ProtectedDestructor { protected: ~ProtectedDestructor() {}};
~PrivateDestructorPrivateDestructor54 struct PrivateDestructor { private: ~PrivateDestructor() {}};
55
~VirtualPublicDestructorVirtualPublicDestructor56 struct VirtualPublicDestructor { public: virtual ~VirtualPublicDestructor() {}};
~VirtualProtectedDestructorVirtualProtectedDestructor57 struct VirtualProtectedDestructor { protected: virtual ~VirtualProtectedDestructor() {}};
~VirtualPrivateDestructorVirtualPrivateDestructor58 struct VirtualPrivateDestructor { private: virtual ~VirtualPrivateDestructor() {}};
59
60 struct PurePublicDestructor { public: virtual ~PurePublicDestructor() = 0; };
61 struct PureProtectedDestructor { protected: virtual ~PureProtectedDestructor() = 0; };
62 struct PurePrivateDestructor { private: virtual ~PurePrivateDestructor() = 0; };
63
64
65 class Empty
66 {
67 };
68
69 union Union {};
70
71 struct bit_zero
72 {
73 int : 0;
74 };
75
76 class Abstract
77 {
78 virtual void foo() = 0;
79 };
80
81 class AbstractDestructor
82 {
83 virtual ~AbstractDestructor() = 0;
84 };
85
86 struct A
87 {
88 ~A();
89 };
90
main()91 int main()
92 {
93 test_is_not_trivially_destructible<void>();
94 test_is_not_trivially_destructible<A>();
95 test_is_not_trivially_destructible<char[]>();
96 test_is_not_trivially_destructible<VirtualPublicDestructor>();
97 test_is_not_trivially_destructible<PurePublicDestructor>();
98
99 test_is_trivially_destructible<Abstract>();
100 test_is_trivially_destructible<Union>();
101 test_is_trivially_destructible<Empty>();
102 test_is_trivially_destructible<int&>();
103 test_is_trivially_destructible<int>();
104 test_is_trivially_destructible<double>();
105 test_is_trivially_destructible<int*>();
106 test_is_trivially_destructible<const int*>();
107 test_is_trivially_destructible<char[3]>();
108 test_is_trivially_destructible<bit_zero>();
109
110 #if TEST_STD_VER >= 11
111 // requires access control sfinae
112 test_is_not_trivially_destructible<ProtectedDestructor>();
113 test_is_not_trivially_destructible<PrivateDestructor>();
114 test_is_not_trivially_destructible<VirtualProtectedDestructor>();
115 test_is_not_trivially_destructible<VirtualPrivateDestructor>();
116 test_is_not_trivially_destructible<PureProtectedDestructor>();
117 test_is_not_trivially_destructible<PurePrivateDestructor>();
118 #endif
119 }
120