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_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 28 29template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible 30 : public integral_constant<bool, __is_convertible_to(_T1, _T2)> {}; 31 32#else // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 33 34namespace __is_convertible_imp 35{ 36template <class _Tp> void __test_convert(_Tp); 37 38template <class _From, class _To, class = void> 39struct __is_convertible_test : public false_type {}; 40 41template <class _From, class _To> 42struct __is_convertible_test<_From, _To, 43 decltype(__is_convertible_imp::__test_convert<_To>(std::declval<_From>()))> : public true_type 44{}; 45 46template <class _Tp, bool _IsArray = is_array<_Tp>::value, 47 bool _IsFunction = is_function<_Tp>::value, 48 bool _IsVoid = is_void<_Tp>::value> 49 struct __is_array_function_or_void {enum {value = 0};}; 50template <class _Tp> struct __is_array_function_or_void<_Tp, true, false, false> {enum {value = 1};}; 51template <class _Tp> struct __is_array_function_or_void<_Tp, false, true, false> {enum {value = 2};}; 52template <class _Tp> struct __is_array_function_or_void<_Tp, false, false, true> {enum {value = 3};}; 53} 54 55template <class _Tp, 56 unsigned = __is_convertible_imp::__is_array_function_or_void<__libcpp_remove_reference_t<_Tp> >::value> 57struct __is_convertible_check 58{ 59 static const size_t __v = 0; 60}; 61 62template <class _Tp> 63struct __is_convertible_check<_Tp, 0> 64{ 65 static const size_t __v = sizeof(_Tp); 66}; 67 68template <class _T1, class _T2, 69 unsigned _T1_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T1>::value, 70 unsigned _T2_is_array_function_or_void = __is_convertible_imp::__is_array_function_or_void<_T2>::value> 71struct __is_convertible 72 : public integral_constant<bool, 73 __is_convertible_imp::__is_convertible_test<_T1, _T2>::value 74 > 75{}; 76 77template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 1> : public false_type {}; 78template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 1> : public false_type {}; 79template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 1> : public false_type {}; 80template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 1> : public false_type {}; 81 82template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 2> : public false_type {}; 83template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 2> : public false_type {}; 84template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 2> : public false_type {}; 85template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 2> : public false_type {}; 86 87template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 0, 3> : public false_type {}; 88template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 1, 3> : public false_type {}; 89template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 2, 3> : public false_type {}; 90template <class _T1, class _T2> struct __is_convertible<_T1, _T2, 3, 3> : public true_type {}; 91 92template <class _T1, class _T2> struct _LIBCPP_TEMPLATE_VIS is_convertible 93 : public __is_convertible<_T1, _T2> 94{ 95 static const size_t __complete_check1 = __is_convertible_check<_T1>::__v; 96 static const size_t __complete_check2 = __is_convertible_check<_T2>::__v; 97}; 98 99#endif // __has_builtin(__is_convertible_to) && !defined(_LIBCPP_USE_IS_CONVERTIBLE_FALLBACK) 100 101#if _LIBCPP_STD_VER > 14 102template <class _From, class _To> 103inline constexpr bool is_convertible_v = is_convertible<_From, _To>::value; 104#endif 105 106_LIBCPP_END_NAMESPACE_STD 107 108#endif // _LIBCPP___TYPE_TRAITS_IS_CONVERTIBLE_H 109