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

Fix comparison operators and get of array #4041

Merged
merged 6 commits into from
Sep 22, 2023
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
41 changes: 32 additions & 9 deletions stl/inc/array
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ _CONSTEXPR20 void swap(array<_Ty, _Size>& _Left, array<_Ty, _Size>& _Right) noex

_EXPORT_STD template <class _Ty, size_t _Size>
_NODISCARD _CONSTEXPR20 bool operator==(const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
return _STD equal(_Left._Unchecked_begin(), _Left._Unchecked_end(), _Right._Unchecked_begin());
return _STD equal(_Left.data(), _Left.data() + _Size, _Right.data());
}

#if !_HAS_CXX20
Expand All @@ -789,14 +789,13 @@ _NODISCARD bool operator!=(const array<_Ty, _Size>& _Left, const array<_Ty, _Siz
_EXPORT_STD template <class _Ty, size_t _Size>
_NODISCARD constexpr _Synth_three_way_result<_Ty> operator<=>(
const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
return _STD lexicographical_compare_three_way(_Left._Unchecked_begin(), _Left._Unchecked_end(),
_Right._Unchecked_begin(), _Right._Unchecked_end(), _Synth_three_way{});
return _STD lexicographical_compare_three_way(
_Left.data(), _Left.data() + _Size, _Right.data(), _Right.data() + _Size, _Synth_three_way{});
}
#else // ^^^ defined(__cpp_lib_concepts) / !defined(__cpp_lib_concepts) vvv
template <class _Ty, size_t _Size>
_NODISCARD _CONSTEXPR20 bool operator<(const array<_Ty, _Size>& _Left, const array<_Ty, _Size>& _Right) {
return _STD lexicographical_compare(
_Left._Unchecked_begin(), _Left._Unchecked_end(), _Right._Unchecked_begin(), _Right._Unchecked_end());
return _STD lexicographical_compare(_Left.data(), _Left.data() + _Size, _Right.data(), _Right.data() + _Size);
}

template <class _Ty, size_t _Size>
Expand Down Expand Up @@ -850,25 +849,49 @@ _NODISCARD constexpr array<remove_cv_t<_Ty>, _Size> to_array(_Ty (&&_Array)[_Siz
_EXPORT_STD template <size_t _Idx, class _Ty, size_t _Size>
_NODISCARD constexpr _Ty& get(array<_Ty, _Size>& _Arr) noexcept {
static_assert(_Idx < _Size, "array index out of bounds");
return _Arr._Elems[_Idx];
if constexpr (_Has_unchecked_begin_end<array<_Ty, _Size>>) {
return _Arr._Elems[_Idx];
} else {
#if _HAS_CXX17
return _Arr[_Idx];
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
return const_cast<_Ty&>(_STD as_const(_Arr)[_Idx]);
#endif // ^^^ !_HAS_CXX17 ^^^
}
}

_EXPORT_STD template <size_t _Idx, class _Ty, size_t _Size>
_NODISCARD constexpr const _Ty& get(const array<_Ty, _Size>& _Arr) noexcept {
static_assert(_Idx < _Size, "array index out of bounds");
return _Arr._Elems[_Idx];
if constexpr (_Has_unchecked_begin_end<array<_Ty, _Size>>) {
return _Arr._Elems[_Idx];
} else {
return _Arr[_Idx];
}
}

_EXPORT_STD template <size_t _Idx, class _Ty, size_t _Size>
_NODISCARD constexpr _Ty&& get(array<_Ty, _Size>&& _Arr) noexcept {
static_assert(_Idx < _Size, "array index out of bounds");
return _STD move(_Arr._Elems[_Idx]);
if constexpr (_Has_unchecked_begin_end<array<_Ty, _Size>>) {
return _STD move(_Arr._Elems[_Idx]);
} else {
#if _HAS_CXX17
return _STD move(_Arr[_Idx]);
#else // ^^^ _HAS_CXX17 / !_HAS_CXX17 vvv
return const_cast<_Ty&&>(_STD move(_STD as_const(_Arr)[_Idx]));
#endif // ^^^ !_HAS_CXX17 ^^^
}
}

_EXPORT_STD template <size_t _Idx, class _Ty, size_t _Size>
_NODISCARD constexpr const _Ty&& get(const array<_Ty, _Size>&& _Arr) noexcept {
static_assert(_Idx < _Size, "array index out of bounds");
return _STD move(_Arr._Elems[_Idx]);
if constexpr (_Has_unchecked_begin_end<array<_Ty, _Size>>) {
return _STD move(_Arr._Elems[_Idx]);
} else {
return _STD move(_Arr[_Idx]);
}
}

#if _HAS_TR1_NAMESPACE
Expand Down
7 changes: 7 additions & 0 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,13 @@ struct _Unused_parameter { // generic unused parameter struct
constexpr _Unused_parameter(_Ty&&) noexcept {}
};

template <class _Ty, class = void> // checks whether a container/view is a non-customized specialization
_INLINE_VAR constexpr bool _Has_unchecked_begin_end = false;

template <class _Ty>
_INLINE_VAR constexpr bool _Has_unchecked_begin_end<_Ty,
void_t<decltype(_STD declval<_Ty&>()._Unchecked_begin()), decltype(_STD declval<_Ty&>()._Unchecked_end())>> = true;

template <class _Ty>
using _Algorithm_int_t = conditional_t<is_integral_v<_Ty>, _Ty, ptrdiff_t>;

Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ tests\GH_003735_char_traits_signatures
tests\GH_003840_tellg_when_reading_lf_file_in_text_mode
tests\GH_003867_output_nan
tests\GH_004023_mdspan_fwd_prod_overflow
tests\GH_004040_container_nonmember_functions
tests\LWG2381_num_get_floating_point
tests\LWG2597_complex_branch_cut
tests\LWG3018_shared_ptr_function
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_matrix.lst
Loading