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