Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libcxx/include/__type_traits/desugars_to.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define _LIBCPP___TYPE_TRAITS_DESUGARS_TO_H

#include <__config>
#include <__type_traits/integral_constant.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down Expand Up @@ -64,6 +65,9 @@ template <class _CanonicalTag, class _Operation, class... _Args>
inline const bool __desugars_to_v<_CanonicalTag, _Operation&&, _Args...> =
__desugars_to_v<_CanonicalTag, _Operation, _Args...>;

template <class _CanonicalTag, class _Operation, class... _Args>
struct __desugars_to : integral_constant<bool, __desugars_to_v<_CanonicalTag, _Operation, _Args...> > {};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___TYPE_TRAITS_DESUGARS_TO_H
32 changes: 26 additions & 6 deletions libcxx/include/__utility/default_three_way_comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _LHS, class _RHS, class = void>
struct __default_three_way_comparator;

template <class _Tp>
struct __default_three_way_comparator<_Tp, _Tp, __enable_if_t<is_arithmetic<_Tp>::value> > {
_LIBCPP_HIDE_FROM_ABI static int operator()(_Tp __lhs, _Tp __rhs) {
template <class _LHS, class _RHS>
struct __default_three_way_comparator<_LHS,
_RHS,
__enable_if_t<is_arithmetic<_LHS>::value && is_arithmetic<_RHS>::value> > {
_LIBCPP_HIDE_FROM_ABI static int operator()(_LHS __lhs, _RHS __rhs) {
if (__lhs < __rhs)
return -1;
if (__lhs > __rhs)
Expand All @@ -38,12 +40,30 @@ struct __default_three_way_comparator<_Tp, _Tp, __enable_if_t<is_arithmetic<_Tp>
}
};

#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_lt_synthesises_from_spaceship)
template <class _LHS, class _RHS>
struct __default_three_way_comparator<
_LHS,
_RHS,
__enable_if_t<!(is_arithmetic<_LHS>::value && is_arithmetic<_RHS>::value) &&
__builtin_lt_synthesises_from_spaceship(const _LHS&, const _RHS&)>> {
_LIBCPP_HIDE_FROM_ABI static int operator()(const _LHS& __lhs, const _RHS& __rhs) {
auto __res = __lhs <=> __rhs;
if (__res < 0)
return -1;
if (__res > 0)
return 1;
return 0;
}
};
#endif

template <class _LHS, class _RHS, bool = true>
inline const bool __has_default_three_way_comparator_v = false;
struct __has_default_three_way_comparator : false_type {};

template <class _LHS, class _RHS>
inline const bool
__has_default_three_way_comparator_v< _LHS, _RHS, sizeof(__default_three_way_comparator<_LHS, _RHS>) >= 0> = true;
struct __has_default_three_way_comparator<_LHS, _RHS, sizeof(__default_three_way_comparator<_LHS, _RHS>) >= 0>
: true_type {};

_LIBCPP_END_NAMESPACE_STD

Expand Down
12 changes: 7 additions & 5 deletions libcxx/include/__utility/lazy_synth_three_way_comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define _LIBCPP___UTILITY_LAZY_SYNTH_THREE_WAY_COMPARATOR_H

#include <__config>
#include <__type_traits/conjunction.h>
#include <__type_traits/desugars_to.h>
#include <__type_traits/enable_if.h>
#include <__utility/default_three_way_comparator.h>
Expand Down Expand Up @@ -69,11 +70,12 @@ struct __eager_compare_result {
};

template <class _Comparator, class _LHS, class _RHS>
struct __lazy_synth_three_way_comparator<_Comparator,
_LHS,
_RHS,
__enable_if_t<__desugars_to_v<__less_tag, _Comparator, _LHS, _RHS> &&
__has_default_three_way_comparator_v<_LHS, _RHS> > > {
struct __lazy_synth_three_way_comparator<
_Comparator,
_LHS,
_RHS,
__enable_if_t<_And<__desugars_to<__less_tag, _Comparator, _LHS, _RHS>,
__has_default_three_way_comparator<_LHS, _RHS> >::value> > {
// This lifetimebound annotation is technically incorrect, but other specializations actually capture the lifetime of
// the comparator.
_LIBCPP_HIDE_FROM_ABI __lazy_synth_three_way_comparator(_LIBCPP_CTOR_LIFETIMEBOUND const _Comparator&) {}
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,7 @@ _LIBCPP_STRING_V1_EXTERN_TEMPLATE_LIST(_LIBCPP_DECLARE, wchar_t)
# endif
# undef _LIBCPP_DECLARE

# if _LIBCPP_STD_VER <= 17 || !__has_builtin(__builtin_lt_synthesises_from_spaceship)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice that the builtin basically removes the need for this special case.

template <class _CharT, class _Traits, class _Alloc>
struct __default_three_way_comparator<basic_string<_CharT, _Traits, _Alloc>, basic_string<_CharT, _Traits, _Alloc> > {
using __string_t _LIBCPP_NODEBUG = basic_string<_CharT, _Traits, _Alloc>;
Expand All @@ -2533,6 +2534,7 @@ struct __default_three_way_comparator<basic_string<_CharT, _Traits, _Alloc>, bas
return __ret;
}
};
# endif

# if _LIBCPP_STD_VER >= 17
template <class _InputIterator,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17

#include <__utility/default_three_way_comparator.h>
#include <string>
#include <vector>

static_assert(std::__has_default_three_way_comparator<int, int>::value);
static_assert(std::__has_default_three_way_comparator<int, long>::value);
static_assert(std::__has_default_three_way_comparator<long, int>::value);
static_assert(std::__has_default_three_way_comparator<long, long>::value);
static_assert(std::__has_default_three_way_comparator<std::string, std::string>::value);

#if __has_builtin(__builtin_lt_synthesises_from_spaceship)
static_assert(std::__has_default_three_way_comparator<const std::string&, const std::string&>::value);
static_assert(std::__has_default_three_way_comparator<const std::string&, const std::string_view&>::value);
static_assert(std::__has_default_three_way_comparator<std::string, std::string_view>::value);
static_assert(std::__has_default_three_way_comparator<const std::string&, const char*>::value);
static_assert(std::__has_default_three_way_comparator<std::string, const char*>::value);
static_assert(!std::__has_default_three_way_comparator<const std::string&, const wchar_t*>::value);

static_assert(std::__has_default_three_way_comparator<const std::vector<int>&, const std::vector<int>&>::value);

struct MyStruct {
int i;

friend auto operator<=>(MyStruct, MyStruct) = default;
};

static_assert(std::__has_default_three_way_comparator<const MyStruct&, const MyStruct&>::value);
#endif
Loading