Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // type_traits |
| 11 | |
| 12 | // is_convertible |
| 13 | |
| 14 | #include <type_traits> |
Marshall Clow | a3e7f52 | 2015-11-30 04:30:02 +0000 | [diff] [blame] | 15 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 34 | typedef void Function(); |
| 35 | typedef char Array[1]; |
| 36 | |
Douglas Gregor | f9e1c7e | 2011-01-25 01:15:41 +0000 | [diff] [blame] | 37 | class NonCopyable { |
| 38 | NonCopyable(NonCopyable&); |
| 39 | }; |
| 40 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 41 | int main() |
| 42 | { |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 53 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 64 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 65 | // Function& |
| 66 | test_is_not_convertible<Function&, void> (); |
| 67 | test_is_not_convertible<Function&, Function> (); |
| 68 | test_is_convertible<Function&, Function&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 69 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 76 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 82 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 88 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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), ""); |
Howard Hinnant | 80f9180 | 2011-01-28 20:00:37 +0000 | [diff] [blame] | 99 | static_assert(( std::is_convertible<const Array, const Array&>::value), ""); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 100 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 101 | test_is_not_convertible<Array, char> (); |
| 102 | test_is_not_convertible<Array, char&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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), ""); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 108 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 121 | test_is_not_convertible<Array&, char> (); |
| 122 | test_is_not_convertible<Array&, char&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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), ""); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 128 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 136 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 137 | test_is_convertible<char, char> (); |
| 138 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 144 | test_is_not_convertible<char, char*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 145 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 153 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 154 | test_is_convertible<char&, char> (); |
| 155 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 161 | test_is_not_convertible<char&, char*> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 162 | |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 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&> (); |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 170 | |
Howard Hinnant | 171771a | 2013-07-08 21:06:38 +0000 | [diff] [blame] | 171 | test_is_not_convertible<char*, char> (); |
| 172 | test_is_not_convertible<char*, char&> (); |
| 173 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 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), ""); |
Marshall Clow | 933afa9 | 2013-07-04 00:10:01 +0000 | [diff] [blame] | 178 | |
| 179 | // NonCopyable |
Douglas Gregor | f9e1c7e | 2011-01-25 01:15:41 +0000 | [diff] [blame] | 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), ""); |
Eric Fiselier | 4bd1546 | 2015-03-30 15:22:20 +0000 | [diff] [blame] | 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. |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 191 | #if __cplusplus >= 201103L || !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) |
Eric Fiselier | 4bd1546 | 2015-03-30 15:22:20 +0000 | [diff] [blame] | 192 | test_is_not_convertible<NonCopyable&, NonCopyable>(); |
| 193 | #endif |
| 194 | |
Howard Hinnant | c52f43e | 2010-08-22 00:59:46 +0000 | [diff] [blame] | 195 | } |