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

Avoid _Iter_diff_t in parameters of std function templates #4249

Merged
merged 4 commits into from
Dec 14, 2023
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
12 changes: 8 additions & 4 deletions stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -5412,7 +5412,8 @@ void random_shuffle(_RanIt _First, _RanIt _Last) { // shuffle [_First, _Last) us

#if _HAS_CXX20
_EXPORT_STD template <class _FwdIt>
constexpr _FwdIt shift_left(_FwdIt _First, const _FwdIt _Last, _Iter_diff_t<_FwdIt> _Pos_to_shift) {
constexpr _FwdIt shift_left(
_FwdIt _First, const _FwdIt _Last, typename iterator_traits<_FwdIt>::difference_type _Pos_to_shift) {
// shift [_First, _Last) left by _Pos_to_shift
// positions; returns the end of the resulting range
_STL_ASSERT(_Pos_to_shift >= 0, "shift count must be non-negative (N4950 [alg.shift]/1)");
Expand Down Expand Up @@ -5446,15 +5447,17 @@ constexpr _FwdIt shift_left(_FwdIt _First, const _FwdIt _Last, _Iter_diff_t<_Fwd
}

_EXPORT_STD template <class _ExPo, class _FwdIt, _Enable_if_execution_policy_t<_ExPo> = 0>
_FwdIt shift_left(_ExPo&&, _FwdIt _First, _FwdIt _Last, _Iter_diff_t<_FwdIt> _Pos_to_shift) noexcept /* terminates */ {
_FwdIt shift_left(_ExPo&&, _FwdIt _First, _FwdIt _Last,
typename iterator_traits<_FwdIt>::difference_type _Pos_to_shift) noexcept /* terminates */ {
// shift [_First, _Last) left by _Pos_to_shift positions
// not parallelized as benchmarks show it isn't worth it
_REQUIRE_CPP17_MUTABLE_ITERATOR(_FwdIt);
return _STD shift_left(_First, _Last, _Pos_to_shift);
}

_EXPORT_STD template <class _FwdIt>
constexpr _FwdIt shift_right(_FwdIt _First, const _FwdIt _Last, _Iter_diff_t<_FwdIt> _Pos_to_shift) {
constexpr _FwdIt shift_right(
_FwdIt _First, const _FwdIt _Last, typename iterator_traits<_FwdIt>::difference_type _Pos_to_shift) {
// shift [_First, _Last) right by _Pos_to_shift
// positions; returns the beginning of the resulting range
_STL_ASSERT(_Pos_to_shift >= 0, "shift count must be non-negative (N4950 [alg.shift]/5)");
Expand Down Expand Up @@ -5528,7 +5531,8 @@ constexpr _FwdIt shift_right(_FwdIt _First, const _FwdIt _Last, _Iter_diff_t<_Fw
}

_EXPORT_STD template <class _ExPo, class _FwdIt, _Enable_if_execution_policy_t<_ExPo> = 0>
_FwdIt shift_right(_ExPo&&, _FwdIt _First, _FwdIt _Last, _Iter_diff_t<_FwdIt> _Pos_to_shift) noexcept /* terminates */ {
_FwdIt shift_right(_ExPo&&, _FwdIt _First, _FwdIt _Last,
typename iterator_traits<_FwdIt>::difference_type _Pos_to_shift) noexcept /* terminates */ {
// shift [_First, _Last) right by _Pos_to_shift positions
// not parallelized as benchmarks show it isn't worth it
_REQUIRE_CPP17_MUTABLE_ITERATOR(_FwdIt);
Expand Down
6 changes: 4 additions & 2 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -1464,7 +1464,8 @@ constexpr _InIt _Next_iter(_InIt _First) { // increment iterator
}

_EXPORT_STD template <class _InIt>
_NODISCARD _CONSTEXPR17 _InIt next(_InIt _First, _Iter_diff_t<_InIt> _Off = 1) { // increment iterator
_NODISCARD _CONSTEXPR17 _InIt next(
_InIt _First, typename iterator_traits<_InIt>::difference_type _Off = 1) { // increment iterator
static_assert(_Is_ranges_input_iter_v<_InIt>, "next requires input iterator");

_STD advance(_First, _Off);
Expand All @@ -1477,7 +1478,8 @@ constexpr _BidIt _Prev_iter(_BidIt _First) { // decrement iterator
}

_EXPORT_STD template <class _BidIt>
_NODISCARD _CONSTEXPR17 _BidIt prev(_BidIt _First, _Iter_diff_t<_BidIt> _Off = 1) { // decrement iterator
_NODISCARD _CONSTEXPR17 _BidIt prev(
_BidIt _First, typename iterator_traits<_BidIt>::difference_type _Off = 1) { // decrement iterator
static_assert(_Is_ranges_bidi_iter_v<_BidIt>, "prev requires bidirectional iterator");

_STD advance(_First, -_Off);
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ tests\VSO_0000000_instantiate_containers
tests\VSO_0000000_instantiate_cvt
tests\VSO_0000000_instantiate_iterators_misc
tests\VSO_0000000_instantiate_type_traits
tests\VSO_1925201_iter_traits
tests\VSO_0000000_list_iterator_debugging
tests\VSO_0000000_list_unique_self_reference
tests\VSO_0000000_matching_npos_address
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/VSO_1925201_iter_traits/env.lst
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 ..\concepts_20_matrix.lst
49 changes: 49 additions & 0 deletions tests/std/tests/VSO_1925201_iter_traits/test.compile.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <algorithm>
#include <concepts>
#include <execution>
#include <iterator>

// Defend against regression of DevCom-10532126, in which several function templates used
// `_Iter_diff_t<meow>` as a parameter type instead of the specified
// `typename iterator_traits<meow>::difference_type. The two are equivalent in C++17, but in C++20
// _Iter_diff_t<meow> becomes iter_difference_t<meow>. We thought the difference was not observable,
// but it interferes with concept overloading.

using std::iter_value_t, std::iterator_traits, std::same_as;
using std::next, std::prev, std::shift_left, std::shift_right;

struct meow {};

constexpr meow* nil = nullptr;

template <class I>
concept Meowerator = same_as<iter_value_t<I>, meow>;

template <Meowerator I>
void next(I, typename iterator_traits<I>::difference_type = 1) {}

template <Meowerator I>
void prev(I, typename iterator_traits<I>::difference_type = 1) {}

template <Meowerator I>
void shift_left(I, I, typename iterator_traits<I>::difference_type) {}
template <Meowerator I>
void shift_right(I, I, typename iterator_traits<I>::difference_type) {}
// Note that we intentionally do not test the ExecutionPolicy overloads of shift_meow. They are
// constrained via an unspecified mechanism to "not participate in overload resolution unless
// is_execution_policy_v<ExecutionPolicy> is true", effectively making concept overloading
// impossible (or at least non-portable).

static_assert(same_as<void, decltype(next(nil))>);
static_assert(same_as<void, decltype(next(nil, 42))>);

static_assert(same_as<void, decltype(prev(nil))>);
static_assert(same_as<void, decltype(prev(nil, 42))>);

#ifndef __clang__ // TRANSITION, LLVM-75404
static_assert(same_as<void, decltype(shift_left(nil, nil, 42))>);
static_assert(same_as<void, decltype(shift_right(nil, nil, 42))>);
#endif // TRANSITION, LLVM-75404