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