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_nothrow_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> 23 void test_is_nothrow_destructible() 24 { 25 static_assert( std::is_nothrow_destructible<T>::value, ""); 26 static_assert( std::is_nothrow_destructible<const T>::value, ""); 27 static_assert( std::is_nothrow_destructible<volatile T>::value, ""); 28 static_assert( std::is_nothrow_destructible<const volatile T>::value, ""); 29 #if TEST_STD_VER > 14 30 static_assert( std::is_nothrow_destructible_v<T>, ""); 31 static_assert( std::is_nothrow_destructible_v<const T>, ""); 32 static_assert( std::is_nothrow_destructible_v<volatile T>, ""); 33 static_assert( std::is_nothrow_destructible_v<const volatile T>, ""); 34 #endif 35 } 36 37 template <class T> 38 void test_is_not_nothrow_destructible() 39 { 40 static_assert(!std::is_nothrow_destructible<T>::value, ""); 41 static_assert(!std::is_nothrow_destructible<const T>::value, ""); 42 static_assert(!std::is_nothrow_destructible<volatile T>::value, ""); 43 static_assert(!std::is_nothrow_destructible<const volatile T>::value, ""); 44 #if TEST_STD_VER > 14 45 static_assert(!std::is_nothrow_destructible_v<T>, ""); 46 static_assert(!std::is_nothrow_destructible_v<const T>, ""); 47 static_assert(!std::is_nothrow_destructible_v<volatile T>, ""); 48 static_assert(!std::is_nothrow_destructible_v<const volatile T>, ""); 49 #endif 50 } 51 52 53 struct PublicDestructor { public: ~PublicDestructor() {}}; 54 struct ProtectedDestructor { protected: ~ProtectedDestructor() {}}; 55 struct PrivateDestructor { private: ~PrivateDestructor() {}}; 56 57 struct VirtualPublicDestructor { public: virtual ~VirtualPublicDestructor() {}}; 58 struct VirtualProtectedDestructor { protected: virtual ~VirtualProtectedDestructor() {}}; 59 struct VirtualPrivateDestructor { private: virtual ~VirtualPrivateDestructor() {}}; 60 61 struct PurePublicDestructor { public: virtual ~PurePublicDestructor() = 0; }; 62 struct PureProtectedDestructor { protected: virtual ~PureProtectedDestructor() = 0; }; 63 struct PurePrivateDestructor { private: virtual ~PurePrivateDestructor() = 0; }; 64 65 class Empty 66 { 67 }; 68 69 70 union Union {}; 71 72 struct bit_zero 73 { 74 int : 0; 75 }; 76 77 class Abstract 78 { 79 virtual void foo() = 0; 80 }; 81 82 83 int main() 84 { 85 test_is_not_nothrow_destructible<void>(); 86 test_is_not_nothrow_destructible<char[]>(); 87 test_is_not_nothrow_destructible<char[][3]>(); 88 89 test_is_nothrow_destructible<int&>(); 90 test_is_nothrow_destructible<int>(); 91 test_is_nothrow_destructible<double>(); 92 test_is_nothrow_destructible<int*>(); 93 test_is_nothrow_destructible<const int*>(); 94 test_is_nothrow_destructible<char[3]>(); 95 96 #if TEST_STD_VER >= 11 97 // requires noexcept. These are all destructible. 98 test_is_nothrow_destructible<PublicDestructor>(); 99 test_is_nothrow_destructible<VirtualPublicDestructor>(); 100 test_is_nothrow_destructible<PurePublicDestructor>(); 101 test_is_nothrow_destructible<bit_zero>(); 102 test_is_nothrow_destructible<Abstract>(); 103 test_is_nothrow_destructible<Empty>(); 104 test_is_nothrow_destructible<Union>(); 105 106 // requires access control 107 test_is_not_nothrow_destructible<ProtectedDestructor>(); 108 test_is_not_nothrow_destructible<PrivateDestructor>(); 109 test_is_not_nothrow_destructible<VirtualProtectedDestructor>(); 110 test_is_not_nothrow_destructible<VirtualPrivateDestructor>(); 111 test_is_not_nothrow_destructible<PureProtectedDestructor>(); 112 test_is_not_nothrow_destructible<PurePrivateDestructor>(); 113 #endif 114 } 115