Skip to content

[libc++] P2944R3: Constrained comparisons - update reference_wrapper implementation #139368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion libcxx/docs/Status/Cxx2cPapers.csv
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"`P2248R8 <https://wg21.link/P2248R8>`__","Enabling list-initialization for algorithms","2024-03 (Tokyo)","","",""
"`P2810R4 <https://wg21.link/P2810R4>`__","``is_debugger_present`` ``is_replaceable``","2024-03 (Tokyo)","","",""
"`P1068R11 <https://wg21.link/P1068R11>`__","Vector API for random number generation","2024-03 (Tokyo)","","",""
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","","Implemented changes to ``reference_wrapper`` and ``pair``"
"`P2944R3 <https://wg21.link/P2944R3>`__","Comparisons for ``reference_wrapper``","2024-03 (Tokyo)","|Partial|","","The changes to ``optional``, ``tuple`` and ``variant`` are not yet implemented"
"`P2642R6 <https://wg21.link/P2642R6>`__","Padded ``mdspan`` layouts","2024-03 (Tokyo)","","",""
"`P3029R1 <https://wg21.link/P3029R1>`__","Better ``mdspan``'s CTAD","2024-03 (Tokyo)","|Complete|","19",""
"","","","","",""
Expand Down
8 changes: 4 additions & 4 deletions libcxx/include/__functional/reference_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#define _LIBCPP___FUNCTIONAL_REFERENCE_WRAPPER_H

#include <__compare/synth_three_way.h>
#include <__concepts/boolean_testable.h>
#include <__config>
#include <__functional/weak_result_type.h>
#include <__memory/addressof.h>
#include <__type_traits/desugars_to.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/invoke.h>
#include <__type_traits/is_const.h>
#include <__type_traits/is_core_convertible.h>
#include <__type_traits/remove_cvref.h>
#include <__type_traits/void_t.h>
#include <__utility/declval.h>
Expand Down Expand Up @@ -75,23 +75,23 @@ class reference_wrapper : public __weak_result_type<_Tp> {

_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper __y)
requires requires {
{ __x.get() == __y.get() } -> __boolean_testable;
{ __x.get() == __y.get() } -> __core_convertible_to<bool>;
}
{
return __x.get() == __y.get();
}

_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, const _Tp& __y)
requires requires {
{ __x.get() == __y } -> __boolean_testable;
{ __x.get() == __y } -> __core_convertible_to<bool>;
}
{
return __x.get() == __y;
}

_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(reference_wrapper __x, reference_wrapper<const _Tp> __y)
requires(!is_const_v<_Tp>) && requires {
{ __x.get() == __y.get() } -> __boolean_testable;
{ __x.get() == __y.get() } -> __core_convertible_to<bool>;
}
{
return __x.get() == __y.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ constexpr std::size_t N{1};
static_assert(std::three_way_comparable<std::array<int, N>>);

// Thanks to SFINAE, the following is not a compiler error but returns `false`
struct NonComparable {};
static_assert(!std::three_way_comparable<std::array<NonComparable, N>>);

// Implementation detail of `test_sequence_container_array_spaceship`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -23,16 +23,13 @@
#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<StrongOrder>>);
static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<WeakOrder>>);
static_assert(HasSpaceshipOperatorWithInt<std::reference_wrapper<PartialOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, int>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, int>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, int>);

static_assert(!HasSpaceshipOperatorWithInt<std::reference_wrapper<NonComparable>>);
static_assert(!HasOperatorSpaceship<std::reference_wrapper<NonComparable>, int>);

// Test comparisons.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -22,17 +22,13 @@

#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(std::three_way_comparable<std::reference_wrapper<StrongOrder>>);
static_assert(std::three_way_comparable<std::reference_wrapper<WeakOrder>>);
static_assert(std::three_way_comparable<std::reference_wrapper<PartialOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>>);

static_assert(!std::three_way_comparable<std::reference_wrapper<NonComparable>>);
static_assert(!HasOperatorSpaceship<std::reference_wrapper<NonComparable>>);

// Test comparisons.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -23,18 +23,15 @@
#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(std::three_way_comparable_with<std::reference_wrapper<StrongOrder>, const StrongOrder>);
static_assert(std::three_way_comparable_with<std::reference_wrapper<WeakOrder>, const WeakOrder>);
static_assert(std::three_way_comparable_with<std::reference_wrapper<PartialOrder>, const PartialOrder>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, std::reference_wrapper<const StrongOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, std::reference_wrapper<const WeakOrder>>);
static_assert(HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, std::reference_wrapper<const PartialOrder>>);

static_assert(!std::three_way_comparable_with<std::reference_wrapper<StrongOrder>, const NonComparable>);
static_assert(!std::three_way_comparable_with<std::reference_wrapper<WeakOrder>, const NonComparable>);
static_assert(!std::three_way_comparable_with<std::reference_wrapper<PartialOrder>, const NonComparable>);
static_assert(!HasOperatorSpaceship<std::reference_wrapper<StrongOrder>, std::reference_wrapper<const NonComparable>>);
static_assert(!HasOperatorSpaceship<std::reference_wrapper<WeakOrder>, std::reference_wrapper<const NonComparable>>);
static_assert(!HasOperatorSpaceship<std::reference_wrapper<PartialOrder>, std::reference_wrapper<const NonComparable>>);

// Test comparisons.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -23,14 +23,13 @@
#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(HasEqualityOperatorWithInt<std::reference_wrapper<EqualityComparable>>);
static_assert(HasOperatorEqual<std::reference_wrapper<EqualityComparable>>);
static_assert(HasOperatorEqual<std::reference_wrapper<EqualityComparable>, int>);

static_assert(!HasEqualityOperatorWithInt<std::reference_wrapper<NonComparable>>);
static_assert(!HasOperatorEqual<std::reference_wrapper<NonComparable>>);
static_assert(!HasOperatorEqual<std::reference_wrapper<NonComparable>, int>);

// Test equality.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -22,14 +22,11 @@
#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(std::equality_comparable<std::reference_wrapper<EqualityComparable>>);
static_assert(HasOperatorEqual<std::reference_wrapper<EqualityComparable>>);

static_assert(!std::equality_comparable<std::reference_wrapper<NonComparable>>);
static_assert(!HasOperatorEqual<std::reference_wrapper<NonComparable>>);

// Test equality.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
// REQUIRES: std-at-least-c++26

// <functional>

Expand All @@ -23,16 +23,13 @@
#include "test_comparisons.h"
#include "test_macros.h"

#include "helper_concepts.h"
#include "helper_types.h"

// Test SFINAE.

static_assert(std::equality_comparable_with<std::reference_wrapper<EqualityComparable>,
std::reference_wrapper<const EqualityComparable>>);
static_assert(
HasOperatorEqual<std::reference_wrapper<EqualityComparable>, std::reference_wrapper<const EqualityComparable>>);

static_assert(!std::equality_comparable_with<std::reference_wrapper<EqualityComparable>,
std::reference_wrapper<const NonComparable>>);
static_assert(
!HasOperatorEqual<std::reference_wrapper<EqualityComparable>, std::reference_wrapper<const NonComparable>>);

// Test equality.

Expand Down

This file was deleted.

This file was deleted.

25 changes: 24 additions & 1 deletion libcxx/test/support/test_comparisons.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,29 @@ struct PartialOrder {
}
};

#endif
template <typename T1, typename T2 = T1>
concept HasOperatorEqual = requires(T1 t1, T2 t2) { t1 == t2; };

template <typename T1, typename T2 = T1>
concept HasOperatorSpaceship = requires(T1 t1, T2 t2) { t1 <=> t2; };

struct NonComparable {};
static_assert(!std::equality_comparable<NonComparable>);
static_assert(!HasOperatorEqual<NonComparable>);
static_assert(!HasOperatorSpaceship<NonComparable>);

class EqualityComparable {
public:
constexpr EqualityComparable(int value) : value_{value} {};

friend constexpr bool operator==(const EqualityComparable&, const EqualityComparable&) noexcept = default;

private:
int value_;
};
static_assert(std::equality_comparable<EqualityComparable>);
static_assert(HasOperatorEqual<EqualityComparable>);

#endif // TEST_STD_VER >= 20

#endif // TEST_COMPARISONS_H
4 changes: 0 additions & 4 deletions libcxx/test/support/test_container_comparisons.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ constexpr bool test_sequence_container_spaceship() {
std::weak_ordering>();

// Thanks to SFINAE, the following is not a compiler error but returns `false`
struct NonComparable {};
static_assert(!std::three_way_comparable<Container<NonComparable>>);

return true;
Expand Down Expand Up @@ -163,7 +162,6 @@ constexpr void test_sequence_container_adaptor_spaceship_with_type() {
template <template <typename...> typename ContainerAdaptor, template <typename...> typename Container>
constexpr bool test_sequence_container_adaptor_spaceship() {
// Thanks to SFINAE, the following is not a compiler error but returns `false`
struct NonComparable {};
static_assert(!std::three_way_comparable<ContainerAdaptor<NonComparable>>);

// The container should fulfill `std::three_way_comparable`
Expand Down Expand Up @@ -301,7 +299,6 @@ constexpr void test_ordered_map_container_spaceship_with_type(Compare comp) {
template <template <typename...> typename Container>
constexpr bool test_ordered_map_container_spaceship() {
// Thanks to SFINAE, the following is not a compiler error but returns `false`
struct NonComparable {};
static_assert(!std::three_way_comparable<Container<int, NonComparable>>);

// The container should fulfill `std::three_way_comparable`
Expand Down Expand Up @@ -444,7 +441,6 @@ constexpr void test_ordered_set_spaceship_with_type(Compare comp) {
template <template <typename...> typename Container>
constexpr bool test_ordered_set_container_spaceship() {
// Thanks to SFINAE, the following is not a compiler error but returns `false`
struct NonComparable {};
static_assert(!std::three_way_comparable<Container<NonComparable>>);

// The container should fulfill `std::three_way_comparable`
Expand Down
Loading