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

Make uninitialized_meow helpers constexpr #1329

Closed
wants to merge 12 commits into from
16 changes: 8 additions & 8 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -1841,9 +1841,9 @@ namespace ranges {
template <bidirectional_iterator _It1, bidirectional_iterator _It2>
requires indirectly_movable<_It1, _It2>
_NODISCARD constexpr _It2 _Move_backward_common(const _It1 _First, _It1 _Last, _It2 _Result) {
if constexpr (_Ptr_move_cat<_It1, _It2>::_Trivially_copyable) {
if constexpr (_Memmove_in_move_is_safe<_It1, _It2>) {
if (!_STD is_constant_evaluated()) {
return _Copy_backward_memmove(_First, _Last, _Result);
return _Memmove_backward(_First, _Last, _Result);
}
}

Expand Down Expand Up @@ -2208,7 +2208,7 @@ namespace ranges {
template <class _InIt1, class _InIt2, class _Pr>
_NODISCARD _CONSTEXPR20 bool _Equal_rev_pred_unchecked(_InIt1 _First1, _InIt2 _First2, const _InIt2 _Last2, _Pr _Pred) {
// compare [_First1, ...) to [_First2, _Last2)
if constexpr (_Equal_memcmp_is_safe<_InIt1, _InIt2, _Pr>) {
if constexpr (_Memcmp_in_equal_is_safe<_InIt1, _InIt2, _Pr>) {
#ifdef __cpp_lib_is_constant_evaluated
if (!_STD is_constant_evaluated())
#endif // __cpp_lib_is_constant_evaluated
Expand All @@ -2229,7 +2229,7 @@ _NODISCARD _CONSTEXPR20 bool _Equal_rev_pred_unchecked(_InIt1 _First1, _InIt2 _F
return true;
}
#else // ^^^ _HAS_IF_CONSTEXPR ^^^ // vvv !_HAS_IF_CONSTEXPR vvv
template <class _InIt1, class _InIt2, class _Pr, enable_if_t<!_Equal_memcmp_is_safe<_InIt1, _InIt2, _Pr>, int> = 0>
template <class _InIt1, class _InIt2, class _Pr, enable_if_t<!_Memcmp_in_equal_is_safe<_InIt1, _InIt2, _Pr>, int> = 0>
bool _Equal_rev_pred_unchecked(_InIt1 _First1, _InIt2 _First2, const _InIt2 _Last2, _Pr _Pred) {
// compare [_First1, ...) to [_First2, _Last2), no special optimization
for (; _First2 != _Last2; ++_First1, (void) ++_First2) {
Expand All @@ -2241,7 +2241,7 @@ bool _Equal_rev_pred_unchecked(_InIt1 _First1, _InIt2 _First2, const _InIt2 _Las
return true;
}

template <class _InIt1, class _InIt2, class _Pr, enable_if_t<_Equal_memcmp_is_safe<_InIt1, _InIt2, _Pr>, int> = 0>
template <class _InIt1, class _InIt2, class _Pr, enable_if_t<_Memcmp_in_equal_is_safe<_InIt1, _InIt2, _Pr>, int> = 0>
bool _Equal_rev_pred_unchecked(const _InIt1 _First1, const _InIt2 _First2, const _InIt2 _Last2, _Pr) {
// compare [_First1, ...) to [_First2, _Last2), memcmp optimization
const auto _First1_ch = reinterpret_cast<const char*>(_First1);
Expand Down Expand Up @@ -2382,7 +2382,7 @@ namespace ranges {
// clang-format off
template <class _It1, class _It2, class _Se2, class _Pr, class _Pj1, class _Pj2>
concept _Equal_rev_pred_can_memcmp = is_same_v<_Pj1, identity> && is_same_v<_Pj2, identity>
&& is_same_v<_Se2, _It2> && _Equal_memcmp_is_safe<_It1, _It2, _Pr>;
&& is_same_v<_Se2, _It2> && _Memcmp_in_equal_is_safe<_It1, _It2, _Pr>;

template <input_iterator _It1, input_iterator _It2, sentinel_for<_It2> _Se2, class _Pr, class _Pj1, class _Pj2>
requires indirectly_comparable<_It1, _It2, _Pr, _Pj1, _Pj2>
Expand Down Expand Up @@ -3929,7 +3929,7 @@ namespace ranges {
_Adl_verify_range(_First, _Last);
auto _UFirst = _Get_unwrapped(_STD move(_First));
const auto _ULast = _Get_unwrapped(_STD move(_Last));
if constexpr (_Fill_memset_is_safe<decltype(_UFirst), _Ty>) {
if constexpr (_Memset_in_fill_is_safe<decltype(_UFirst), _Ty>) {
#ifdef __cpp_lib_is_constant_evaluated
if (!_STD is_constant_evaluated())
#endif // __cpp_lib_is_constant_evaluated
Expand Down Expand Up @@ -3969,7 +3969,7 @@ namespace ranges {
constexpr _It operator()(_It _First, iter_difference_t<_It> _Count, const _Ty& _Value) const {
if (_Count > 0) {
auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
if constexpr (_Fill_memset_is_safe<decltype(_UFirst), _Ty>) {
if constexpr (_Memset_in_fill_is_safe<decltype(_UFirst), _Ty>) {
#ifdef __cpp_lib_is_constant_evaluated
if (!_STD is_constant_evaluated())
#endif // __cpp_lib_is_constant_evaluated
Expand Down
48 changes: 25 additions & 23 deletions stl/inc/memory
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ namespace ranges {
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_OSe, _Out>);
_STL_INTERNAL_STATIC_ASSERT(constructible_from<iter_value_t<_Out>, iter_reference_t<_It>>);

if constexpr (is_same_v<_Se, _It> && _Ptr_copy_cat<_It, _Out>::_Really_trivial) {
return _Copy_memcpy_common(_IFirst, _ILast, _OFirst, _OLast);
if constexpr (is_same_v<_Se, _It> && _Memmove_in_uninitialized_copy_is_safe<_It, _Out>) {
return _Memmove_forward_common(_IFirst, _ILast, _OFirst, _OLast);
} else {
_Uninitialized_backout _Backout{_STD move(_OFirst)};

Expand Down Expand Up @@ -137,8 +137,8 @@ _NoThrowFwdIt uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _

auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_copy_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
if constexpr (_Memmove_in_uninitialized_copy_is_safe<decltype(_UFirst), decltype(_UDest)>) {
_UDest = _Memmove_forward(_UFirst, _UFirst + _Count, _UDest);
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};

Expand Down Expand Up @@ -168,7 +168,7 @@ _NoThrowFwdIt _Uninitialized_copy_n_unchecked2(_InIt _First, _Diff _Count, const
template <class _InIt, class _Diff, class _NoThrowFwdIt>
_NoThrowFwdIt _Uninitialized_copy_n_unchecked2(const _InIt _First, const _Diff _Count, const _NoThrowFwdIt _Dest,
true_type) { // copy [_First, _First + _Count) to [_Dest, ...), memmove optimization
return _Copy_memmove(_First, _First + _Count, _Dest);
return _Memmove_forward(_First, _First + _Count, _Dest);
}

template <class _InIt, class _Diff, class _NoThrowFwdIt>
Expand All @@ -181,8 +181,9 @@ _NoThrowFwdIt uninitialized_copy_n(const _InIt _First, const _Diff _Count_raw, _

auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
_Seek_wrapped(_Dest, _Uninitialized_copy_n_unchecked2(_UFirst, _Count, _UDest,
bool_constant<_Ptr_copy_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial>{}));
_Seek_wrapped(
_Dest, _Uninitialized_copy_n_unchecked2(_UFirst, _Count, _UDest,
bool_constant<_Memmove_in_uninitialized_copy_is_safe<decltype(_UFirst), decltype(_UDest)>>{}));
return _Dest;
}
#endif // _HAS_IF_CONSTEXPR
Expand Down Expand Up @@ -212,8 +213,8 @@ namespace ranges {
auto _IFirst = _Get_unwrapped_n(_STD move(_First1), _Count);
auto _OFirst = _Get_unwrapped(_STD move(_First2));
const auto _OLast = _Get_unwrapped(_STD move(_Last2));
if constexpr (_Ptr_copy_cat<_It, _Out>::_Really_trivial) {
_OFirst = _Copy_memcpy_common(_IFirst, _IFirst + _Count, _OFirst, _OLast);
if constexpr (_Memmove_in_uninitialized_copy_is_safe<_It, _Out>) {
_OFirst = _Memmove_forward_common(_IFirst, _IFirst + _Count, _OFirst, _OLast);
} else {
_Uninitialized_backout _Backout{_STD move(_OFirst)};

Expand Down Expand Up @@ -299,8 +300,8 @@ namespace ranges {
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_OSe, _Out>);
_STL_INTERNAL_STATIC_ASSERT(constructible_from<iter_value_t<_Out>, iter_rvalue_reference_t<_It>>);

if constexpr (is_same_v<_Se, _It> && _Ptr_move_cat<_It, _Out>::_Really_trivial) {
return _Copy_memcpy_common(_IFirst, _ILast, _OFirst, _OLast);
if constexpr (is_same_v<_Se, _It> && _Memmove_in_uninitialized_move_is_safe<_It, _Out>) {
return _Memmove_forward_common(_IFirst, _ILast, _OFirst, _OLast);
} else {
_Uninitialized_backout _Backout{_STD move(_OFirst)};

Expand Down Expand Up @@ -328,8 +329,8 @@ pair<_InIt, _NoThrowFwdIt> uninitialized_move_n(_InIt _First, const _Diff _Count

auto _UFirst = _Get_unwrapped_n(_First, _Count);
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
if constexpr (_Ptr_move_cat<decltype(_UFirst), decltype(_UDest)>::_Really_trivial) {
_UDest = _Copy_memmove(_UFirst, _UFirst + _Count, _UDest);
if constexpr (_Memmove_in_uninitialized_move_is_safe<decltype(_UFirst), decltype(_UDest)>) {
_UDest = _Memmove_forward(_UFirst, _UFirst + _Count, _UDest);
_UFirst += _Count;
} else {
_Uninitialized_backout<decltype(_UDest)> _Backout{_UDest};
Expand Down Expand Up @@ -373,8 +374,8 @@ namespace ranges {
auto _IFirst = _Get_unwrapped_n(_STD move(_First1), _Count);
auto _OFirst = _Get_unwrapped(_STD move(_First2));
const auto _OLast = _Get_unwrapped(_STD move(_Last2));
if constexpr (_Ptr_move_cat<_It, _Out>::_Really_trivial) {
_OFirst = _Copy_memcpy_common(_IFirst, _IFirst + _Count, _OFirst, _OLast);
if constexpr (_Memmove_in_uninitialized_move_is_safe<_It, _Out>) {
_OFirst = _Memmove_forward_common(_IFirst, _IFirst + _Count, _OFirst, _OLast);
} else {
_Uninitialized_backout _Backout{_STD move(_OFirst)};

Expand Down Expand Up @@ -426,7 +427,7 @@ namespace ranges {
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_Se, _It>);
_STL_INTERNAL_STATIC_ASSERT(constructible_from<iter_value_t<_It>, const _Ty&>);

if constexpr (_Fill_memset_is_safe<_It, _Ty>) {
if constexpr (_Memset_in_fill_is_safe<_It, _Ty>) {
const auto _OFinal = _RANGES next(_OFirst, _STD move(_OLast));
const auto _Diff = static_cast<size_t>(_OFinal - _OFirst);
_Fill_memset(_OFirst, _Val, _Diff);
Expand Down Expand Up @@ -458,7 +459,7 @@ _NoThrowFwdIt uninitialized_fill_n(_NoThrowFwdIt _First, const _Diff _Count_raw,
}

auto _UFirst = _Get_unwrapped_n(_First, _Count);
if constexpr (_Fill_memset_is_safe<_Unwrapped_n_t<const _NoThrowFwdIt&>, _Tval>) {
if constexpr (_Memset_in_fill_is_safe<_Unwrapped_n_t<const _NoThrowFwdIt&>, _Tval>) {
_Fill_memset(_UFirst, _Val, static_cast<size_t>(_Count));
_UFirst += _Count;
} else {
Expand Down Expand Up @@ -505,7 +506,7 @@ _NoThrowFwdIt uninitialized_fill_n(_NoThrowFwdIt _First, const _Diff _Count_raw,

auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
_Seek_wrapped(_First, _Uninitialized_fill_n_unchecked1(_STD move(_UFirst), _Count, _Val,
bool_constant<_Fill_memset_is_safe<_Unwrapped_t<_NoThrowFwdIt>, _Tval>>{}));
bool_constant<_Memset_in_fill_is_safe<_Unwrapped_t<_NoThrowFwdIt>, _Tval>>{}));
return _First;
}
#endif // _HAS_IF_CONSTEXPR
Expand All @@ -527,7 +528,7 @@ namespace ranges {
}

auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
if constexpr (_Fill_memset_is_safe<_It, _Ty>) {
if constexpr (_Memset_in_fill_is_safe<_It, _Ty>) {
_Fill_memset(_UFirst, _Val, static_cast<size_t>(_Count));
_Seek_wrapped(_First, _UFirst + _Count);
} else {
Expand Down Expand Up @@ -2320,7 +2321,7 @@ struct _Reverse_destroy_multidimensional_n_guard {
template <class _Ty, size_t _Size>
void _Uninitialized_copy_multidimensional(const _Ty (&_In)[_Size], _Ty (&_Out)[_Size]) {
if constexpr (is_trivial_v<_Ty>) {
_Copy_memmove(_In, _In + _Size, _Out);
_Memmove_forward(_In, _In + _Size, _Out);
} else if constexpr (is_array_v<_Ty>) {
_Reverse_destroy_multidimensional_n_guard<_Ty> _Guard{_Out, 0};
for (size_t& _Idx = _Guard._Index; _Idx < _Size; ++_Idx) {
Expand Down Expand Up @@ -2383,7 +2384,7 @@ void _Uninitialized_fill_multidimensional_n(_Ty* const _Out, const size_t _Size,
_Uninitialized_copy_multidimensional(_Val, _Out[_Idx]); // intentionally copy, not fill
}
_Guard._Target = nullptr;
} else if constexpr (_Fill_memset_is_safe<_Ty*, _Ty>) {
} else if constexpr (_Memset_in_fill_is_safe<_Ty*, _Ty>) {
_Fill_memset(_Out, _Val, _Size);
} else {
_Uninitialized_rev_destroying_backout _Backout{_Out};
Expand Down Expand Up @@ -2683,7 +2684,7 @@ template <class _Ty, size_t _Size, class _Alloc>
void _Uninitialized_copy_multidimensional_al(const _Ty (&_In)[_Size], _Ty (&_Out)[_Size], _Alloc& _Al) {
using _Item = remove_all_extents_t<_Ty>;
if constexpr (conjunction_v<is_trivial<_Ty>, _Uses_default_construct<_Alloc, _Item*, const _Item&>>) {
_Copy_memmove(_In, _In + _Size, _Out);
_Memmove_forward(_In, _In + _Size, _Out);
} else if constexpr (is_array_v<_Ty>) {
_Reverse_destroy_multidimensional_n_al_guard<_Ty, _Alloc> _Guard{_Out, 0, _Al};
for (size_t& _Idx = _Guard._Index; _Idx < _Size; ++_Idx) {
Expand Down Expand Up @@ -2727,7 +2728,8 @@ void _Uninitialized_fill_multidimensional_n_al(_Ty* const _Out, const size_t _Si
_Uninitialized_copy_multidimensional_al(_Val, _Out[_Idx], _Al); // intentionally copy, not fill
}
_Guard._Target = nullptr;
} else if constexpr (_Fill_memset_is_safe<_Ty*, _Ty> && _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
} else if constexpr (_Memset_in_fill_is_safe<_Ty*,
_Ty> && _Uses_default_construct<_Alloc, _Ty*, const _Ty&>::value) {
_Fill_memset(_Out, _Val, _Size);
} else {
_Uninitialized_rev_destroying_backout_al _Backout{_Out, _Al};
Expand Down
8 changes: 4 additions & 4 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,13 @@ private:

const auto _Oldcapacity = static_cast<size_type>(_My_data._Myend - _Myfirst);
#if _HAS_IF_CONSTEXPR
if constexpr (conjunction_v<bool_constant<_Ptr_copy_cat<_Ty*, _Ty*>::_Trivially_copyable>,
if constexpr (conjunction_v<bool_constant<_Memmove_in_copy_is_safe<_Ty*, _Ty*>>,
_Uses_default_construct<_Alty, _Ty*, _Ty>, _Uses_default_destroy<_Alty, _Ty*>>) {
if (_Newsize > _Oldcapacity) {
_Clear_and_reserve_geometric(_Newsize);
}

_Mylast = _Refancy<pointer>(_Copy_memmove(_Unfancy(_First), _Unfancy(_Last), _Unfancy(_Myfirst)));
_Mylast = _Refancy<pointer>(_Memmove_forward(_Unfancy(_First), _Unfancy(_Last), _Unfancy(_Myfirst)));
} else
#endif // _HAS_IF_CONSTEXPR
{
Expand Down Expand Up @@ -1108,15 +1108,15 @@ private:
_My_data._Orphan_all();

#if _HAS_IF_CONSTEXPR
if constexpr (conjunction_v<bool_constant<_Ptr_copy_cat<_Iter, _Ty*>::_Trivially_copyable>,
if constexpr (conjunction_v<bool_constant<_Memmove_in_copy_is_safe<_Iter, _Ty*>>,
_Uses_default_construct<_Alty, _Ty*, decltype(*_First)>,
_Uses_default_destroy<_Alty, _Ty*>>) {
const auto _Oldcapacity = static_cast<size_type>(_Myend - _Myfirst);
if (_Newsize > _Oldcapacity) {
_Clear_and_reserve_geometric(_Newsize);
}

_Mylast = _Refancy<pointer>(_Copy_memmove(_First, _Last, _Unfancy(_Myfirst)));
_Mylast = _Refancy<pointer>(_Memmove_forward(_First, _Last, _Unfancy(_Myfirst)));
} else
#endif // _HAS_IF_CONSTEXPR
{
Expand Down
Loading