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 #ifndef _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H 10 #define _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H 11 12 #include <__config> 13 #include <__type_traits/integral_constant.h> 14 #include <__type_traits/is_array.h> 15 #include <__type_traits/is_function.h> 16 #include <__type_traits/is_void.h> 17 #include <__type_traits/remove_reference.h> 18 #include <__utility/declval.h> 19 #include <cstddef> 20 21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 22 # pragma GCC system_header 23 #endif 24 25 _LIBCPP_BEGIN_NAMESPACE_STD 26 27 #if __has_builtin(__is_convertible) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 28 29 template <class _T1, class _T2> 30 struct _LIBCPP_TEMPLATE_VIS is_convertible : public integral_constant<bool, __is_convertible(_T1, _T2)> {}; 31 32 #elif __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 33 34 template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible 35 : public integral_constant<bool, __is_convertible_to(_T1, _T2)> {}; 36 37 // TODO: Remove this fallback when GCC < 13 support is no longer required. 38 // GCC 13 has the __is_convertible built-in. 39 #else // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 40 41 namespace __is_convertible_imp 42 { 43 template <class _Tp> void __test_convert(_Tp); 44 45 template <class _From, class _To, class = void> 46 struct __is_convertible_test : public false_type {}; 47 48 template <class _From, class _To> 49 struct __is_convertible_test<_From, _To, 50 decltype(__is_convertible_imp::__test_convert<_To>(std::declval<_From>()))> : public true_type 51 {}; 52 53 template <class _Tp, bool _IsArray = is_array<_Tp>::value, 54 bool _IsFunction = is_function<_Tp>::value, 55 bool _IsVoid = is_void<_Tp>::value> 56 struct __is_array_function_or_void {enum {value = 0};}; 57 template <class _Tp> struct __is_array_function_or_void<_Tp, true, false, false> {enum {value = 1};}; 58 template <class _Tp> struct __is_array_function_or_void<_Tp, false, true, false> {enum {value = 2};}; 59 template <class _Tp> struct __is_array_function_or_void<_Tp, false, false, true> {enum {value = 3};}; 60 } 61 62 template <class _Tp, 63 unsigned = __is_convertible_imp::__is_array_function_or_void<__libcpp_remove_reference_t<_Tp> >::value> 64 struct __is_convertible_check 65 { 66 static const size_t __v = 0; 67 }; 68 69 template <class _Tp> 70 struct __is_convertible_check<_Tp, 0> 71 { 72 static const size_t __v = sizeof(_Tp); 73 }; 74 75 template <class _T1, class _T2, 76 unsigned _T1_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T1>::value, 77 unsigned _T2_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T2>::value> 78 struct __is_convertible 79 : public integral_constant<bool, 80 __is_convertible_imp::__is_convertible_test<_T1, _T2>::value 81 > 82 {}; 83 84 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 1> : public false_type {}; 85 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 1> : public false_type {}; 86 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 1> : public false_type {}; 87 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 1> : public false_type {}; 88 89 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 2> : public false_type {}; 90 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 2> : public false_type {}; 91 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 2> : public false_type {}; 92 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 2> : public false_type {}; 93 94 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 3> : public false_type {}; 95 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 3> : public false_type {}; 96 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 3> : public false_type {}; 97 template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 3> : public true_type {}; 98 99 template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible 100 : public __is_convertible<_T1, _T2> 101 { 102 static const size_t __complete_check1 = __is_convertible_check<_T1>::__v; 103 static const size_t __complete_check2 = __is_convertible_check<_T2>::__v; 104 }; 105 106 #endif // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 107 108 #if _LIBCPP_STD_VER > 14 109 template <class _From, class _To> 110 inline constexpr bool is_convertible_v = is_convertible<_From, _To>::value; 111 #endif 112 113 _LIBCPP_END_NAMESPACE_STD 114 115 #endif // _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H 116