Skip to content

Commit

Permalink
Enable binary operators for tuple
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Hoeflinger <[email protected]>
  • Loading branch information
danhoeflinger committed Mar 29, 2024
1 parent f489ff4 commit e212145
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions include/oneapi/dpl/pstl/tuple_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,16 +424,36 @@ struct tuple<T1, T...>
return *this;
}

friend bool
operator==(const tuple& __lhs, const tuple& __rhs)
{
return __lhs.holder.value == __rhs.holder.value && __lhs.next == __rhs.next;
}
friend bool
operator!=(const tuple& __lhs, const tuple& __rhs)
{
return !(__lhs == __rhs);
}
// This macro is used to define binary operators for comparison between oneAPI::dpl::__internal::tuple and
// std::tuple when they have the same number of template arguments and the individual elements which are invdividually
// comparable. These overloads are required because for overload resolution of template arguments, exact match is
// required and the compiler does not consider implicit conversions. We also allow std::tuple implementation to
// implement the operators themselves, which guarantees a match with oneAPI::dpl::__internal::tuple
#define _TUPLE_BINARY_OPERATOR_OVERLOAD(__OPERATOR) \
template <typename _U1, typename... _U, typename = ::std::enable_if_t<(sizeof...(_U) == sizeof...(T))>> \
friend bool operator __OPERATOR(const tuple& __lhs, const oneapi::dpl::__internal::tuple<_U1, _U...>& __rhs) \
{ \
return static_cast<std::tuple<T1, T...>>(__lhs) __OPERATOR static_cast<std::tuple<_U1, _U...>>(__rhs); \
} \
template <typename _U1, typename... _U, typename = ::std::enable_if_t<(sizeof...(_U) == sizeof...(T))>> \
friend bool operator __OPERATOR(const tuple& __lhs, const std::tuple<_U1, _U...>& __rhs) \
{ \
return static_cast<std::tuple<T1, T...>>(__lhs) __OPERATOR __rhs; \
} \
template <typename _U1, typename... _U, typename = ::std::enable_if_t<(sizeof...(_U) == sizeof...(T))>> \
friend bool operator __OPERATOR(const std::tuple<_U1, _U...>& __lhs, const tuple& __rhs) \
{ \
return __lhs __OPERATOR static_cast<std::tuple<T1, T...>>(__rhs); \
}

_TUPLE_BINARY_OPERATOR_OVERLOAD(==)
_TUPLE_BINARY_OPERATOR_OVERLOAD(!=)
_TUPLE_BINARY_OPERATOR_OVERLOAD(<)
_TUPLE_BINARY_OPERATOR_OVERLOAD(<=)
_TUPLE_BINARY_OPERATOR_OVERLOAD(>)
_TUPLE_BINARY_OPERATOR_OVERLOAD(>=)

#undef _TUPLE_BINARY_OPERATOR_OVERLOAD

template <typename U1, typename... U, ::std::size_t... _Ip>
static ::std::tuple<U1, U...>
Expand Down Expand Up @@ -463,11 +483,6 @@ struct tuple<>
{
return *this;
}
friend bool
operator==(const tuple& /*__lhs*/, const tuple& /*__rhs*/)
{
return true;
}
};

inline void
Expand Down

0 comments on commit e212145

Please sign in to comment.