Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 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 | #include <type_traits> |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 15 | |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 16 | #include "test_macros.h" |
| 17 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 18 | template <class T> |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 19 | void test_is_destructible() |
| 20 | { |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 21 | static_assert( std::is_destructible<T>::value, ""); |
| 22 | static_assert( std::is_destructible<const T>::value, ""); |
| 23 | static_assert( std::is_destructible<volatile T>::value, ""); |
| 24 | static_assert( std::is_destructible<const volatile T>::value, ""); |
| 25 | } |
| 26 | |
| 27 | template <class T> |
| 28 | void test_is_not_destructible() |
| 29 | { |
| 30 | static_assert(!std::is_destructible<T>::value, ""); |
| 31 | static_assert(!std::is_destructible<const T>::value, ""); |
| 32 | static_assert(!std::is_destructible<volatile T>::value, ""); |
| 33 | static_assert(!std::is_destructible<const volatile T>::value, ""); |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 36 | class Empty {}; |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 37 | |
| 38 | class NotEmpty |
| 39 | { |
| 40 | virtual ~NotEmpty(); |
| 41 | }; |
| 42 | |
| 43 | union Union {}; |
| 44 | |
| 45 | struct bit_zero |
| 46 | { |
| 47 | int : 0; |
| 48 | }; |
| 49 | |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 50 | struct A |
| 51 | { |
| 52 | ~A(); |
| 53 | }; |
| 54 | |
Howard Hinnant | d179407 | 2013-08-30 19:12:42 +0000 | [diff] [blame] | 55 | typedef void (Function) (); |
| 56 | |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 57 | struct PublicAbstract { public: virtual void foo() = 0; }; |
| 58 | struct ProtectedAbstract { protected: virtual void foo() = 0; }; |
| 59 | struct PrivateAbstract { private: virtual void foo() = 0; }; |
| 60 | |
| 61 | struct PublicDestructor { public: ~PublicDestructor() {}}; |
| 62 | struct ProtectedDestructor { protected: ~ProtectedDestructor() {}}; |
| 63 | struct PrivateDestructor { private: ~PrivateDestructor() {}}; |
| 64 | |
| 65 | struct VirtualPublicDestructor { public: virtual ~VirtualPublicDestructor() {}}; |
| 66 | struct VirtualProtectedDestructor { protected: virtual ~VirtualProtectedDestructor() {}}; |
| 67 | struct VirtualPrivateDestructor { private: virtual ~VirtualPrivateDestructor() {}}; |
| 68 | |
| 69 | struct PurePublicDestructor { public: virtual ~PurePublicDestructor() = 0; }; |
| 70 | struct PureProtectedDestructor { protected: virtual ~PureProtectedDestructor() = 0; }; |
| 71 | struct PurePrivateDestructor { private: virtual ~PurePrivateDestructor() = 0; }; |
| 72 | |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 73 | #if TEST_STD_VER >= 11 |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 74 | struct DeletedPublicDestructor { public: ~DeletedPublicDestructor() = delete; }; |
| 75 | struct DeletedProtectedDestructor { protected: ~DeletedProtectedDestructor() = delete; }; |
| 76 | struct DeletedPrivateDestructor { private: ~DeletedPrivateDestructor() = delete; }; |
| 77 | |
| 78 | struct DeletedVirtualPublicDestructor { public: virtual ~DeletedVirtualPublicDestructor() = delete; }; |
| 79 | struct DeletedVirtualProtectedDestructor { protected: virtual ~DeletedVirtualProtectedDestructor() = delete; }; |
| 80 | struct DeletedVirtualPrivateDestructor { private: virtual ~DeletedVirtualPrivateDestructor() = delete; }; |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 81 | #endif |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 82 | |
| 83 | |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 84 | int main() |
| 85 | { |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 86 | test_is_destructible<A>(); |
| 87 | test_is_destructible<int&>(); |
| 88 | test_is_destructible<Union>(); |
| 89 | test_is_destructible<Empty>(); |
| 90 | test_is_destructible<int>(); |
| 91 | test_is_destructible<double>(); |
| 92 | test_is_destructible<int*>(); |
| 93 | test_is_destructible<const int*>(); |
| 94 | test_is_destructible<char[3]>(); |
| 95 | test_is_destructible<bit_zero>(); |
Howard Hinnant | 80e19ac | 2013-08-09 16:53:45 +0000 | [diff] [blame] | 96 | test_is_destructible<int[3]>(); |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 97 | test_is_destructible<ProtectedAbstract>(); |
| 98 | test_is_destructible<PublicAbstract>(); |
| 99 | test_is_destructible<PrivateAbstract>(); |
| 100 | test_is_destructible<PublicDestructor>(); |
| 101 | test_is_destructible<VirtualPublicDestructor>(); |
| 102 | test_is_destructible<PurePublicDestructor>(); |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 103 | |
Howard Hinnant | 80e19ac | 2013-08-09 16:53:45 +0000 | [diff] [blame] | 104 | test_is_not_destructible<int[]>(); |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 105 | test_is_not_destructible<void>(); |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 106 | test_is_not_destructible<Function>(); |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 107 | |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 108 | #if TEST_STD_VER >= 11 |
| 109 | // Test access controlled destructors |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 110 | test_is_not_destructible<ProtectedDestructor>(); |
| 111 | test_is_not_destructible<PrivateDestructor>(); |
| 112 | test_is_not_destructible<VirtualProtectedDestructor>(); |
| 113 | test_is_not_destructible<VirtualPrivateDestructor>(); |
| 114 | test_is_not_destructible<PureProtectedDestructor>(); |
| 115 | test_is_not_destructible<PurePrivateDestructor>(); |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 116 | |
| 117 | // Test deleted constructors |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 118 | test_is_not_destructible<DeletedPublicDestructor>(); |
| 119 | test_is_not_destructible<DeletedProtectedDestructor>(); |
| 120 | test_is_not_destructible<DeletedPrivateDestructor>(); |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 121 | //test_is_not_destructible<DeletedVirtualPublicDestructor>(); // previously failed due to clang bug #20268 |
Marshall Clow | 2b44e3d | 2014-07-16 15:51:50 +0000 | [diff] [blame] | 122 | test_is_not_destructible<DeletedVirtualProtectedDestructor>(); |
| 123 | test_is_not_destructible<DeletedVirtualPrivateDestructor>(); |
| 124 | |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 125 | // Test private destructors |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 126 | test_is_not_destructible<NotEmpty>(); |
| 127 | #endif |
Eric Fiselier | 12c6d9c | 2015-07-18 16:43:58 +0000 | [diff] [blame] | 128 | |
Howard Hinnant | 1468b66 | 2010-11-19 22:17:28 +0000 | [diff] [blame] | 129 | } |