• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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 
23 template <class T>
test_is_destructible()24 void test_is_destructible()
25 {
26     static_assert( std::is_destructible<T>::value, "");
27     static_assert( std::is_destructible<const T>::value, "");
28     static_assert( std::is_destructible<volatile T>::value, "");
29     static_assert( std::is_destructible<const volatile T>::value, "");
30 #if TEST_STD_VER > 14
31     static_assert( std::is_destructible_v<T>, "");
32     static_assert( std::is_destructible_v<const T>, "");
33     static_assert( std::is_destructible_v<volatile T>, "");
34     static_assert( std::is_destructible_v<const volatile T>, "");
35 #endif
36 }
37 
38 template <class T>
test_is_not_destructible()39 void test_is_not_destructible()
40 {
41     static_assert(!std::is_destructible<T>::value, "");
42     static_assert(!std::is_destructible<const T>::value, "");
43     static_assert(!std::is_destructible<volatile T>::value, "");
44     static_assert(!std::is_destructible<const volatile T>::value, "");
45 #if TEST_STD_VER > 14
46     static_assert(!std::is_destructible_v<T>, "");
47     static_assert(!std::is_destructible_v<const T>, "");
48     static_assert(!std::is_destructible_v<volatile T>, "");
49     static_assert(!std::is_destructible_v<const volatile T>, "");
50 #endif
51 }
52 
53 class Empty {};
54 
55 class NotEmpty
56 {
57     virtual ~NotEmpty();
58 };
59 
60 union Union {};
61 
62 struct bit_zero
63 {
64     int :  0;
65 };
66 
67 struct A
68 {
69     ~A();
70 };
71 
72 typedef void (Function) ();
73 
74 struct PublicAbstract                    { public:    virtual void foo() = 0; };
75 struct ProtectedAbstract                 { protected: virtual void foo() = 0; };
76 struct PrivateAbstract                   { private:   virtual void foo() = 0; };
77 
~PublicDestructorPublicDestructor78 struct PublicDestructor                  { public:    ~PublicDestructor() {}};
~ProtectedDestructorProtectedDestructor79 struct ProtectedDestructor               { protected: ~ProtectedDestructor() {}};
~PrivateDestructorPrivateDestructor80 struct PrivateDestructor                 { private:   ~PrivateDestructor() {}};
81 
~VirtualPublicDestructorVirtualPublicDestructor82 struct VirtualPublicDestructor           { public:    virtual ~VirtualPublicDestructor() {}};
~VirtualProtectedDestructorVirtualProtectedDestructor83 struct VirtualProtectedDestructor        { protected: virtual ~VirtualProtectedDestructor() {}};
~VirtualPrivateDestructorVirtualPrivateDestructor84 struct VirtualPrivateDestructor          { private:   virtual ~VirtualPrivateDestructor() {}};
85 
86 struct PurePublicDestructor              { public:    virtual ~PurePublicDestructor() = 0; };
87 struct PureProtectedDestructor           { protected: virtual ~PureProtectedDestructor() = 0; };
88 struct PurePrivateDestructor             { private:   virtual ~PurePrivateDestructor() = 0; };
89 
90 #if TEST_STD_VER >= 11
91 struct DeletedPublicDestructor           { public:    ~DeletedPublicDestructor() = delete; };
92 struct DeletedProtectedDestructor        { protected: ~DeletedProtectedDestructor() = delete; };
93 struct DeletedPrivateDestructor          { private:   ~DeletedPrivateDestructor() = delete; };
94 
95 struct DeletedVirtualPublicDestructor    { public:    virtual ~DeletedVirtualPublicDestructor() = delete; };
96 struct DeletedVirtualProtectedDestructor { protected: virtual ~DeletedVirtualProtectedDestructor() = delete; };
97 struct DeletedVirtualPrivateDestructor   { private:   virtual ~DeletedVirtualPrivateDestructor() = delete; };
98 #endif
99 
100 
main()101 int main()
102 {
103     test_is_destructible<A>();
104     test_is_destructible<int&>();
105     test_is_destructible<Union>();
106     test_is_destructible<Empty>();
107     test_is_destructible<int>();
108     test_is_destructible<double>();
109     test_is_destructible<int*>();
110     test_is_destructible<const int*>();
111     test_is_destructible<char[3]>();
112     test_is_destructible<bit_zero>();
113     test_is_destructible<int[3]>();
114     test_is_destructible<ProtectedAbstract>();
115     test_is_destructible<PublicAbstract>();
116     test_is_destructible<PrivateAbstract>();
117     test_is_destructible<PublicDestructor>();
118     test_is_destructible<VirtualPublicDestructor>();
119     test_is_destructible<PurePublicDestructor>();
120 
121     test_is_not_destructible<int[]>();
122     test_is_not_destructible<void>();
123     test_is_not_destructible<Function>();
124 
125 #if TEST_STD_VER >= 11
126     // Test access controlled destructors
127     test_is_not_destructible<ProtectedDestructor>();
128     test_is_not_destructible<PrivateDestructor>();
129     test_is_not_destructible<VirtualProtectedDestructor>();
130     test_is_not_destructible<VirtualPrivateDestructor>();
131     test_is_not_destructible<PureProtectedDestructor>();
132     test_is_not_destructible<PurePrivateDestructor>();
133 
134     // Test deleted constructors
135     test_is_not_destructible<DeletedPublicDestructor>();
136     test_is_not_destructible<DeletedProtectedDestructor>();
137     test_is_not_destructible<DeletedPrivateDestructor>();
138     //test_is_not_destructible<DeletedVirtualPublicDestructor>(); // previously failed due to clang bug #20268
139     test_is_not_destructible<DeletedVirtualProtectedDestructor>();
140     test_is_not_destructible<DeletedVirtualPrivateDestructor>();
141 
142     // Test private destructors
143     test_is_not_destructible<NotEmpty>();
144 #endif
145 
146 }
147