Skip to content

Commit

Permalink
DRY QList and QVLA operator<=>() implementation
Browse files Browse the repository at this point in the history
Both QList and QVLA defined an if_has_op_less_or_op_compare_three_way
trait. The only difference was that QList's implementation was using
QTypeTraits::has_operator_less_than_container<>, and thus required a
Container template parameter, while QVLA didn't need it.

However, QVLA would still work properly with the _container version
of the trait, so move the QList's implementation into
QtOrderingPrivate and use it consistently in both classes.

Task-number: QTBUG-120305
Change-Id: I1e4415ae5a25d4faad218bbad979a49ce851d557
Reviewed-by: Marc Mutz <[email protected]>
  • Loading branch information
isolovev committed Dec 5, 2024
1 parent 9cbfa8c commit 3a8a8b4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/corelib/global/qcomparehelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,13 @@ constexpr bool compareThreeWayNoexcept() noexcept
}
}
} synthThreeWay;

template <typename Container, typename T>
using if_has_op_less_or_op_compare_three_way =
std::enable_if_t<
std::disjunction_v<QTypeTraits::has_operator_less_than_container<Container, T>,
QTypeTraits::has_operator_compare_three_way<T>>,
bool>;
#endif // __cpp_lib_three_way_comparison

// These checks do not use Qt::compareThreeWay(), so only work for user-defined
Expand Down
10 changes: 2 additions & 8 deletions src/corelib/tools/qlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,8 @@ class QList
}

#if defined(__cpp_lib_three_way_comparison) && defined(__cpp_lib_concepts)
template <typename Container, typename U = T>
using if_has_op_less_or_op_compare_three_way =
std::enable_if_t<
std::disjunction_v<QTypeTraits::has_operator_less_than_container<Container, U>,
QTypeTraits::has_operator_compare_three_way<U>>,
bool>;

template <typename U = T, if_has_op_less_or_op_compare_three_way<QList, U> = true>
template <typename U = T,
QtOrderingPrivate::if_has_op_less_or_op_compare_three_way<QList, U> = true>
friend auto operator<=>(const QList &lhs, const QList &rhs)
{
return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(),
Expand Down
9 changes: 1 addition & 8 deletions src/corelib/tools/qvarlengtharray.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,8 @@ class QVarLengthArray
}

#if defined(__cpp_lib_three_way_comparison) && defined(__cpp_lib_concepts)
template <typename U = T>
using if_has_op_less_or_op_compare_three_way =
std::enable_if_t<
std::disjunction_v<QTypeTraits::has_operator_less_than<U>,
QTypeTraits::has_operator_compare_three_way<U>>,
bool>;

template <typename U = T, qsizetype Prealloc2 = Prealloc,
if_has_op_less_or_op_compare_three_way<U> = true>
QtOrderingPrivate::if_has_op_less_or_op_compare_three_way<QVarLengthArray, U> = true>
friend auto
operator<=>(const QVarLengthArray &lhs, const QVarLengthArray<T, Prealloc2> &rhs)
{
Expand Down

0 comments on commit 3a8a8b4

Please sign in to comment.