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