This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of New status.
drop_view::begin const
may have 𝒪(n) complexitySection: 25.7.12.2 [range.drop.view] Status: New Submitter: Hewill Kang Opened: 2023-11-08 Last modified: 2023-11-18
Priority: Not Prioritized
View other active issues in [range.drop.view].
View all other issues in [range.drop.view].
View all issues with New status.
Discussion:
drop_view::begin const
is specified to return ranges::next(ranges::begin(base_),
count_, ranges::end(base_))
, which has 𝒪(n) complexity when base_
is a random-access-sized but non-common range (demo):
#include <ranges>
int main() {
const auto s = std::ranges::subrange(std::views::iota(0uz), size_t(-1));
const auto r = std::ranges::drop_view(s, s.size() - 1);
const auto b = r.begin(); // time out
}
Proposed resolution:
This wording is relative to N4964.
Modify 25.7.12.2 [range.drop.view] as indicated:
constexpr auto begin() requires (!(simple-view<V> && random_access_range<const V> && sized_range<const V>));constexpr auto begin() const requires random_access_range<const V> && sized_range<const V>;
-3- Returns:
(?.?) — If
V
modelsrandom_access_range
andsized_range
,ranges::begin(base_) + (ranges::distance(base_) - range_difference_t<V>(size()))
(?.?) — Otherwise,
ranges::next(ranges::begin(base_), count_, ranges::end(base_))
.-4- Remarks: In order to provide the amortized constant-time complexity required by the
[Note 1: Without this, applying arange
concept whenV
does not modeldrop_view
srandom_access_range
andsized_range
, this functionforward_range
the first overloadcaches the result within thedrop_view
for use on subsequent calls.reverse_view
over adrop_view
would have quadratic iteration complexity. — end note]
constexpr auto begin() const requires random_access_range<const V> && sized_range<const V>;
-?- Returns:
ranges::begin(base_) + (ranges::distance(base_) - range_difference_t<const V>(size()))
.