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 2 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
57 changes: 48 additions & 9 deletions stl/inc/variant
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#include <xsmf_control.h>
#include <xstddef>

#ifdef _HAS_CXX20
#include <compare>
#include <concepts>
#endif // _HAS_CXX20

d-winsor marked this conversation as resolved.
Show resolved Hide resolved
#pragma pack(push, _CRT_PACKING)
#pragma warning(push, _STL_WARNING_LEVEL)
#pragma warning(disable : _STL_DISABLED_WARNINGS)
Expand Down Expand Up @@ -1365,13 +1370,14 @@ _NODISCARD constexpr add_pointer_t<const _Ty> get_if(
}

// RELATIONAL OPERATORS [variant.relops]
template <class _Op, class... _Types>
template <class _Op, class _Result, class... _Types>
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
struct _Variant_relop_visitor { // 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 +1393,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_visitor<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 +1403,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_visitor<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 +1414,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_visitor<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 +1427,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_visitor<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 +1440,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_visitor<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 +1453,39 @@ _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_visitor<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 _HAS_CXX20
struct _Variant_relop_3way_comparison {
template <class _Ty>
compare_three_way_result_t<_Ty> operator()(const _Ty& _Left, const _Ty& _Right) {
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
return _Left <=> _Right;
}
};

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>...>,
_Variant_relop_3way_comparison, const _Types&, const _Types&>...>) /* strengthened */ {
// determine the three-way comparison of _Left's and _Right's index, if equal
// return the three-way comparison of the contained value of _Left and _Right.
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
using _Visitor = _Variant_relop_visitor<_Variant_relop_3way_comparison,
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 // _HAS_CXX20

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

#if !_HAS_CXX20
_NODISCARD constexpr bool operator!=(monostate, monostate) noexcept {
return false;
}
Expand All @@ -1713,6 +1746,12 @@ _NODISCARD constexpr bool operator<=(monostate, monostate) noexcept {
_NODISCARD constexpr bool operator>=(monostate, monostate) noexcept {
return true;
}
#else // ^^^ _HAS_CXX20 vvv
d-winsor marked this conversation as resolved.
Show resolved Hide resolved

_NODISCARD constexpr strong_ordering operator<=>(monostate, monostate) noexcept {
return strong_ordering::equal;
}
#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, "");
d-winsor marked this conversation as resolved.
Show resolved Hide resolved
}
{ // slice
std::slice a1(2, 3, 4);
std::slice a2(2, 3, 4);
Expand Down