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_nothrow_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_nothrow_destructible()22 void test_is_nothrow_destructible()
23 {
24 static_assert( std::is_nothrow_destructible<T>::value, "");
25 static_assert( std::is_nothrow_destructible<const T>::value, "");
26 static_assert( std::is_nothrow_destructible<volatile T>::value, "");
27 static_assert( std::is_nothrow_destructible<const volatile T>::value, "");
28 #if TEST_STD_VER > 14
29 static_assert( std::is_nothrow_destructible_v<T>, "");
30 static_assert( std::is_nothrow_destructible_v<const T>, "");
31 static_assert( std::is_nothrow_destructible_v<volatile T>, "");
32 static_assert( std::is_nothrow_destructible_v<const volatile T>, "");
33 #endif
34 }
35
36 template <class T>
test_is_not_nothrow_destructible()37 void test_is_not_nothrow_destructible()
38 {
39 static_assert(!std::is_nothrow_destructible<T>::value, "");
40 static_assert(!std::is_nothrow_destructible<const T>::value, "");
41 static_assert(!std::is_nothrow_destructible<volatile T>::value, "");
42 static_assert(!std::is_nothrow_destructible<const volatile T>::value, "");
43 #if TEST_STD_VER > 14
44 static_assert(!std::is_nothrow_destructible_v<T>, "");
45 static_assert(!std::is_nothrow_destructible_v<const T>, "");
46 static_assert(!std::is_nothrow_destructible_v<volatile T>, "");
47 static_assert(!std::is_nothrow_destructible_v<const volatile T>, "");
48 #endif
49 }
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 class Empty
65 {
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
main(int,char **)82 int main(int, char**)
83 {
84 test_is_not_nothrow_destructible<void>();
85 test_is_not_nothrow_destructible<char[]>();
86 test_is_not_nothrow_destructible<char[][3]>();
87
88 test_is_nothrow_destructible<int&>();
89 test_is_nothrow_destructible<int>();
90 test_is_nothrow_destructible<double>();
91 test_is_nothrow_destructible<int*>();
92 test_is_nothrow_destructible<const int*>();
93 test_is_nothrow_destructible<char[3]>();
94
95 #if TEST_STD_VER >= 11
96 // requires noexcept. These are all destructible.
97 test_is_nothrow_destructible<PublicDestructor>();
98 test_is_nothrow_destructible<VirtualPublicDestructor>();
99 test_is_nothrow_destructible<PurePublicDestructor>();
100 test_is_nothrow_destructible<bit_zero>();
101 test_is_nothrow_destructible<Abstract>();
102 test_is_nothrow_destructible<Empty>();
103 test_is_nothrow_destructible<Union>();
104 #endif
105 // requires access control
106 test_is_not_nothrow_destructible<ProtectedDestructor>();
107 test_is_not_nothrow_destructible<PrivateDestructor>();
108 test_is_not_nothrow_destructible<VirtualProtectedDestructor>();
109 test_is_not_nothrow_destructible<VirtualPrivateDestructor>();
110 test_is_not_nothrow_destructible<PureProtectedDestructor>();
111 test_is_not_nothrow_destructible<PurePrivateDestructor>();
112
113
114 return 0;
115 }
116