Skip to content
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

feature/spaceship: Clause 20: variant, monostate #1657

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 36 additions & 10 deletions stl/inc/variant
Original file line number Diff line number Diff line change
Expand Up @@ -1365,13 +1365,14 @@ _NODISCARD constexpr add_pointer_t<const _Ty> get_if(
}

// RELATIONAL OPERATORS [variant.relops]
template <class _Op, class... _Types>
struct _Variant_relop_visitor { // evaluate _Op with the contained value of two variants that hold the same alternative
template <class _Op, class _Result, class... _Types>
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
struct _Variant_relop_visitor2 { // evaluate _Op with the contained value of two variants that hold the same alternative
const _Variant_storage<_Types...>& _Left;

template <class _Ty, size_t _Idx>
_NODISCARD constexpr bool operator()(_Tagged<const _Ty&, _Idx> _Right) const noexcept(
disjunction_v<bool_constant<_Idx == variant_npos>, is_nothrow_invocable_r<bool, _Op, const _Ty&, const _Ty&>>) {
_NODISCARD constexpr _Result operator()(_Tagged<const _Ty&, _Idx> _Right) const
noexcept(disjunction_v<bool_constant<_Idx == variant_npos>,
is_nothrow_invocable_r<_Result, _Op, const _Ty&, const _Ty&>>) {
// determine the relationship between the stored values of _Left and _Right
// pre: _Left.index() == _Idx && _Right.index() == _Idx
if constexpr (_Idx != variant_npos) {
Expand All @@ -1387,7 +1388,7 @@ template <class... _Types>
_NODISCARD constexpr bool operator==(const variant<_Types...>& _Left, const variant<_Types...>& _Right) noexcept(
conjunction_v<is_nothrow_invocable_r<bool, equal_to<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if the arguments are both valueless or contain equal values
using _Visitor = _Variant_relop_visitor<equal_to<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<equal_to<>, bool, _Types...>;
const size_t _Right_index = _Right.index();
return _Left.index() == _Right_index
&& _Variant_raw_visit(_Right_index, _Right._Storage(), _Visitor{_Left._Storage()});
Expand All @@ -1397,7 +1398,7 @@ template <class... _Types>
_NODISCARD constexpr bool operator!=(const variant<_Types...>& _Left, const variant<_Types...>& _Right) noexcept(
conjunction_v<is_nothrow_invocable_r<bool, not_equal_to<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if the arguments have different active alternatives or contain unequal values
using _Visitor = _Variant_relop_visitor<not_equal_to<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<not_equal_to<>, bool, _Types...>;
const size_t _Right_index = _Right.index();
return _Left.index() != _Right_index
|| _Variant_raw_visit(_Right_index, _Right._Storage(), _Visitor{_Left._Storage()});
Expand All @@ -1408,7 +1409,7 @@ _NODISCARD constexpr bool operator<(const variant<_Types...>& _Left, const varia
conjunction_v<is_nothrow_invocable_r<bool, less<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if _Left has a lesser index(), or equal index() and lesser
// contained value than _Right
using _Visitor = _Variant_relop_visitor<less<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<less<>, bool, _Types...>;
const size_t _Left_offset = _Left.index() + 1;
const size_t _Right_offset = _Right.index() + 1;
return _Left_offset < _Right_offset
Expand All @@ -1421,7 +1422,7 @@ _NODISCARD constexpr bool operator>(const variant<_Types...>& _Left, const varia
conjunction_v<is_nothrow_invocable_r<bool, greater<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if _Left has a greater index(), or equal index() and
// greater contained value than _Right
using _Visitor = _Variant_relop_visitor<greater<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<greater<>, bool, _Types...>;
const size_t _Left_offset = _Left.index() + 1;
const size_t _Right_offset = _Right.index() + 1;
return _Left_offset > _Right_offset
Expand All @@ -1434,7 +1435,7 @@ _NODISCARD constexpr bool operator<=(const variant<_Types...>& _Left, const vari
conjunction_v<is_nothrow_invocable_r<bool, less_equal<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if _Left's index() is less than _Right's, or equal and
// _Left contains a value less than or equal to _Right
using _Visitor = _Variant_relop_visitor<less_equal<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<less_equal<>, bool, _Types...>;
const size_t _Left_offset = _Left.index() + 1;
const size_t _Right_offset = _Right.index() + 1;
return _Left_offset < _Right_offset
Expand All @@ -1447,14 +1448,32 @@ _NODISCARD constexpr bool operator>=(const variant<_Types...>& _Left, const vari
conjunction_v<is_nothrow_invocable_r<bool, greater_equal<>, const _Types&, const _Types&>...>) /* strengthened */ {
// determine if _Left's index() is greater than _Right's, or equal and
// _Left contains a value greater than or equal to _Right
using _Visitor = _Variant_relop_visitor<greater_equal<>, _Types...>;
using _Visitor = _Variant_relop_visitor2<greater_equal<>, bool, _Types...>;
const size_t _Left_offset = _Left.index() + 1;
const size_t _Right_offset = _Right.index() + 1;
return _Left_offset > _Right_offset
|| (_Left_offset == _Right_offset
&& _Variant_raw_visit(_Right_offset - 1, _Right._Storage(), _Visitor{_Left._Storage()}));
}

#ifdef __cpp_lib_concepts
template <class... _Types>
_NODISCARD constexpr common_comparison_category_t<compare_three_way_result_t<_Types>...>
operator<=>(const variant<_Types...>& _Left, const variant<_Types...>& _Right) noexcept(
conjunction_v<is_nothrow_invocable_r<common_comparison_category_t<compare_three_way_result_t<_Types>...>,
compare_three_way, const _Types&, const _Types&>...>) /* strengthened */ {
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
// Determine the three-way comparison of _Left's and _Right's index, if equal
// return the three-way comparison of the contained values of _Left and _Right
using _Visitor = _Variant_relop_visitor2<compare_three_way,
common_comparison_category_t<compare_three_way_result_t<_Types>...>, _Types...>;
const size_t _Left_offset = _Left.index() + 1;
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
const size_t _Right_offset = _Right.index() + 1;
const auto _Offset_order = _Left_offset <=> _Right_offset;
return _Offset_order != 0 ? _Offset_order
: _Variant_raw_visit(_Right_offset - 1, _Right._Storage(), _Visitor{_Left._Storage()});
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
}
#endif // __cpp_lib_concepts

// VISITATION [variant.visit]
template <class... _Variants>
inline constexpr size_t _Variant_total_states =
Expand Down Expand Up @@ -1698,6 +1717,12 @@ struct monostate {};
_NODISCARD constexpr bool operator==(monostate, monostate) noexcept {
return true;
}

#if _HAS_CXX20
_NODISCARD constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
return strong_ordering::equal;
}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD constexpr bool operator!=(monostate, monostate) noexcept {
return false;
}
Expand All @@ -1713,6 +1738,7 @@ _NODISCARD constexpr bool operator<=(monostate, monostate) noexcept {
_NODISCARD constexpr bool operator>=(monostate, monostate) noexcept {
return true;
}
#endif // !_HAS_CXX20

// SPECIALIZED ALGORITHMS [variant.specalg]
template <class... _Types,
Expand Down
17 changes: 17 additions & 0 deletions tests/std/tests/P1614R2_spaceship/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unordered_set>
#include <utility>
#include <valarray>
#include <variant>
#include <vector>

template <class T>
Expand Down Expand Up @@ -484,6 +485,22 @@ void ordering_test_cases() {

spaceship_test<std::strong_ordering>(c_mem[0], c_mem[0], c_mem[1]);
}
{ // variant
using V = std::variant<int, long>;
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
constexpr V v0_0(std::in_place_index<0>, 0);
constexpr V v0_1(std::in_place_index<0>, 1);
constexpr V v1_0(std::in_place_index<1>, 0);
constexpr V v1_1(std::in_place_index<1>, 1);

spaceship_test<std::strong_ordering>(v0_0, v0_0, v0_1);
spaceship_test<std::strong_ordering>(v0_1, v0_1, v1_0);
spaceship_test<std::strong_ordering>(v1_0, v1_0, v1_1);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

using M = std::monostate;
constexpr M m1{};
constexpr M m2{};
static_assert((m1 <=> m2) == 0);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
}
{ // slice
std::slice a1(2, 3, 4);
std::slice a2(2, 3, 4);
Expand Down