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_convertible 13 14 #include <type_traits> 15 16 template <class T, class U> 17 void test_is_convertible() 18 { 19 static_assert((std::is_convertible<T, U>::value), ""); 20 static_assert((std::is_convertible<const T, U>::value), ""); 21 static_assert((std::is_convertible<T, const U>::value), ""); 22 static_assert((std::is_convertible<const T, const U>::value), ""); 23 } 24 25 template <class T, class U> 26 void test_is_not_convertible() 27 { 28 static_assert((!std::is_convertible<T, U>::value), ""); 29 static_assert((!std::is_convertible<const T, U>::value), ""); 30 static_assert((!std::is_convertible<T, const U>::value), ""); 31 static_assert((!std::is_convertible<const T, const U>::value), ""); 32 } 33 34 typedef void Function(); 35 typedef char Array[1]; 36 37 class NonCopyable { 38 NonCopyable(NonCopyable&); 39 }; 40 41 int main() 42 { 43 // void 44 test_is_convertible<void,void> (); 45 test_is_not_convertible<void,Function> (); 46 test_is_not_convertible<void,Function&> (); 47 test_is_not_convertible<void,Function*> (); 48 test_is_not_convertible<void,Array> (); 49 test_is_not_convertible<void,Array&> (); 50 test_is_not_convertible<void,char> (); 51 test_is_not_convertible<void,char&> (); 52 test_is_not_convertible<void,char*> (); 53 54 // Function 55 test_is_not_convertible<Function, void> (); 56 test_is_not_convertible<Function, Function> (); 57 test_is_convertible<Function, Function&> (); 58 test_is_convertible<Function, Function*> (); 59 test_is_not_convertible<Function, Array> (); 60 test_is_not_convertible<Function, Array&> (); 61 test_is_not_convertible<Function, char> (); 62 test_is_not_convertible<Function, char&> (); 63 test_is_not_convertible<Function, char*> (); 64 65 // Function& 66 test_is_not_convertible<Function&, void> (); 67 test_is_not_convertible<Function&, Function> (); 68 test_is_convertible<Function&, Function&> (); 69 70 test_is_convertible<Function&, Function*> (); 71 test_is_not_convertible<Function&, Array> (); 72 test_is_not_convertible<Function&, Array&> (); 73 test_is_not_convertible<Function&, char> (); 74 test_is_not_convertible<Function&, char&> (); 75 test_is_not_convertible<Function&, char*> (); 76 77 // Function* 78 test_is_not_convertible<Function*, void> (); 79 test_is_not_convertible<Function*, Function> (); 80 test_is_not_convertible<Function*, Function&> (); 81 test_is_convertible<Function*, Function*> (); 82 83 test_is_not_convertible<Function*, Array> (); 84 test_is_not_convertible<Function*, Array&> (); 85 test_is_not_convertible<Function*, char> (); 86 test_is_not_convertible<Function*, char&> (); 87 test_is_not_convertible<Function*, char*> (); 88 89 // Array 90 test_is_not_convertible<Array, void> (); 91 test_is_not_convertible<Array, Function> (); 92 test_is_not_convertible<Array, Function&> (); 93 test_is_not_convertible<Array, Function*> (); 94 test_is_not_convertible<Array, Array> (); 95 96 static_assert((!std::is_convertible<Array, Array&>::value), ""); 97 static_assert(( std::is_convertible<Array, const Array&>::value), ""); 98 static_assert((!std::is_convertible<const Array, Array&>::value), ""); 99 static_assert(( std::is_convertible<const Array, const Array&>::value), ""); 100 101 test_is_not_convertible<Array, char> (); 102 test_is_not_convertible<Array, char&> (); 103 104 static_assert(( std::is_convertible<Array, char*>::value), ""); 105 static_assert(( std::is_convertible<Array, const char*>::value), ""); 106 static_assert((!std::is_convertible<const Array, char*>::value), ""); 107 static_assert(( std::is_convertible<const Array, const char*>::value), ""); 108 109 // Array& 110 test_is_not_convertible<Array&, void> (); 111 test_is_not_convertible<Array&, Function> (); 112 test_is_not_convertible<Array&, Function&> (); 113 test_is_not_convertible<Array&, Function*> (); 114 test_is_not_convertible<Array&, Array> (); 115 116 static_assert(( std::is_convertible<Array&, Array&>::value), ""); 117 static_assert(( std::is_convertible<Array&, const Array&>::value), ""); 118 static_assert((!std::is_convertible<const Array&, Array&>::value), ""); 119 static_assert(( std::is_convertible<const Array&, const Array&>::value), ""); 120 121 test_is_not_convertible<Array&, char> (); 122 test_is_not_convertible<Array&, char&> (); 123 124 static_assert(( std::is_convertible<Array&, char*>::value), ""); 125 static_assert(( std::is_convertible<Array&, const char*>::value), ""); 126 static_assert((!std::is_convertible<const Array&, char*>::value), ""); 127 static_assert(( std::is_convertible<const Array&, const char*>::value), ""); 128 129 // char 130 test_is_not_convertible<char, void> (); 131 test_is_not_convertible<char, Function> (); 132 test_is_not_convertible<char, Function&> (); 133 test_is_not_convertible<char, Function*> (); 134 test_is_not_convertible<char, Array> (); 135 test_is_not_convertible<char, Array&> (); 136 137 test_is_convertible<char, char> (); 138 139 static_assert((!std::is_convertible<char, char&>::value), ""); 140 static_assert(( std::is_convertible<char, const char&>::value), ""); 141 static_assert((!std::is_convertible<const char, char&>::value), ""); 142 static_assert(( std::is_convertible<const char, const char&>::value), ""); 143 144 test_is_not_convertible<char, char*> (); 145 146 // char& 147 test_is_not_convertible<char&, void> (); 148 test_is_not_convertible<char&, Function> (); 149 test_is_not_convertible<char&, Function&> (); 150 test_is_not_convertible<char&, Function*> (); 151 test_is_not_convertible<char&, Array> (); 152 test_is_not_convertible<char&, Array&> (); 153 154 test_is_convertible<char&, char> (); 155 156 static_assert(( std::is_convertible<char&, char&>::value), ""); 157 static_assert(( std::is_convertible<char&, const char&>::value), ""); 158 static_assert((!std::is_convertible<const char&, char&>::value), ""); 159 static_assert(( std::is_convertible<const char&, const char&>::value), ""); 160 161 test_is_not_convertible<char&, char*> (); 162 163 // char* 164 test_is_not_convertible<char*, void> (); 165 test_is_not_convertible<char*, Function> (); 166 test_is_not_convertible<char*, Function&> (); 167 test_is_not_convertible<char*, Function*> (); 168 test_is_not_convertible<char*, Array> (); 169 test_is_not_convertible<char*, Array&> (); 170 171 test_is_not_convertible<char*, char> (); 172 test_is_not_convertible<char*, char&> (); 173 174 static_assert(( std::is_convertible<char*, char*>::value), ""); 175 static_assert(( std::is_convertible<char*, const char*>::value), ""); 176 static_assert((!std::is_convertible<const char*, char*>::value), ""); 177 static_assert(( std::is_convertible<const char*, const char*>::value), ""); 178 179 // NonCopyable 180 static_assert((std::is_convertible<NonCopyable&, NonCopyable&>::value), ""); 181 static_assert((std::is_convertible<NonCopyable&, const NonCopyable&>::value), ""); 182 static_assert((std::is_convertible<NonCopyable&, const volatile NonCopyable&>::value), ""); 183 static_assert((std::is_convertible<NonCopyable&, volatile NonCopyable&>::value), ""); 184 static_assert((std::is_convertible<const NonCopyable&, const NonCopyable&>::value), ""); 185 static_assert((std::is_convertible<const NonCopyable&, const volatile NonCopyable&>::value), ""); 186 static_assert((std::is_convertible<volatile NonCopyable&, const volatile NonCopyable&>::value), ""); 187 static_assert((std::is_convertible<const volatile NonCopyable&, const volatile NonCopyable&>::value), ""); 188 static_assert((!std::is_convertible<const NonCopyable&, NonCopyable&>::value), ""); 189 // This test requires Access control SFINAE which we only have in C++11 or when 190 // we are using the compiler builtin for is_convertible. 191 #if __cplusplus >= 201103L || !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 192 test_is_not_convertible<NonCopyable&, NonCopyable>(); 193 #endif 194 195 } 196