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_EQUALITY_COMPARABLE_H 10 #define _LIBCPP___TYPE_TRAITS_IS_EQUALITY_COMPARABLE_H 11 12 #include <__config> 13 #include <__type_traits/enable_if.h> 14 #include <__type_traits/integral_constant.h> 15 #include <__type_traits/is_integral.h> 16 #include <__type_traits/is_same.h> 17 #include <__type_traits/is_signed.h> 18 #include <__type_traits/is_void.h> 19 #include <__type_traits/remove_cv.h> 20 #include <__type_traits/remove_cvref.h> 21 #include <__type_traits/void_t.h> 22 #include <__utility/declval.h> 23 24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 25 # pragma GCC system_header 26 #endif 27 28 _LIBCPP_BEGIN_NAMESPACE_STD 29 30 template <class _Tp, class _Up, class = void> 31 struct __is_equality_comparable : false_type {}; 32 33 template <class _Tp, class _Up> 34 struct __is_equality_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() == std::declval<_Up>())> > : true_type { 35 }; 36 37 // A type is_trivially_equality_comparable if the expression `a == b` is equivalent to `std::memcmp(&a, &b, sizeof(T))` 38 // (with `a` and `b` being of type `T`). For the case where we compare two object of the same type, we can use 39 // __is_trivially_equality_comparable. We have special-casing for pointers which point to the same type ignoring 40 // cv-qualifications and comparing to void-pointers. 41 // 42 // The following types are not trivially equality comparable: 43 // floating-point types: different bit-patterns can compare equal. (e.g 0.0 and -0.0) 44 // enums: The user is allowed to specialize operator== for enums 45 // pointers that don't have the same type (ignoring cv-qualifiers): pointers to virtual bases are equality comparable, 46 // but don't have the same bit-pattern. An exception to this is comparing to a void-pointer. There the bit-pattern is 47 // always compared. 48 49 template <class _Tp, class _Up, class = void> 50 struct __libcpp_is_trivially_equality_comparable_impl : false_type {}; 51 52 template <class _Tp> 53 struct __libcpp_is_trivially_equality_comparable_impl<_Tp, _Tp> 54 #if __has_builtin(__is_trivially_equality_comparable) 55 : integral_constant<bool, __is_trivially_equality_comparable(_Tp) && __is_equality_comparable<_Tp, _Tp>::value> { 56 }; 57 #else 58 : is_integral<_Tp> { 59 }; 60 #endif // __has_builtin(__is_trivially_equality_comparable) 61 62 template <class _Tp, class _Up> 63 struct __libcpp_is_trivially_equality_comparable_impl< 64 _Tp, 65 _Up, 66 __enable_if_t<is_integral<_Tp>::value && is_integral<_Up>::value && !is_same<_Tp, _Up>::value && 67 is_signed<_Tp>::value == is_signed<_Up>::value && sizeof(_Tp) == sizeof(_Up)> > : true_type {}; 68 69 template <class _Tp> 70 struct __libcpp_is_trivially_equality_comparable_impl<_Tp*, _Tp*> : true_type {}; 71 72 // TODO: Use is_pointer_inverconvertible_base_of 73 template <class _Tp, class _Up> 74 struct __libcpp_is_trivially_equality_comparable_impl<_Tp*, _Up*> 75 : integral_constant< 76 bool, 77 __is_equality_comparable<_Tp*, _Up*>::value && 78 (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value || is_void<_Tp>::value || is_void<_Up>::value)> { 79 }; 80 81 template <class _Tp, class _Up> 82 using __libcpp_is_trivially_equality_comparable = 83 __libcpp_is_trivially_equality_comparable_impl<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >; 84 85 _LIBCPP_END_NAMESPACE_STD 86 87 #endif // _LIBCPP___TYPE_TRAITS_IS_EQUALITY_COMPARABLE_H 88