ranges::to
should reserve when sized_sentinel_for
is satisfiedSection: 25.5.7.2 [range.utility.conv.to] Status: New Submitter: Hewill Kang Opened: 2024-04-13 Last modified: 2024-04-16
Priority: Not Prioritized
View other active issues in [range.utility.conv.to].
View all other issues in [range.utility.conv.to].
View all issues with New status.
Discussion:
ranges::to
currently only reserves when r
satisfies sized_range
.
However, we can also extract its size when r
is an input_range
that its sentinel-iterator
pair satisfies sized_sentinel_for
.
ranges::begin(r)
anyway.
Proposed resolution:
This wording is relative to N4981.
Modify 25.5.7.2 [range.utility.conv.to] as indicated:
template<class C, input_range R, class... Args> requires (!view<C>) constexpr C to(R&& r, Args&&... args);-1- Mandates:
C
is a cv-unqualified class type.-2- Returns: An object of type
C
constructed from the elements ofr
in the following manner:
(2.1) — […]
(2.1.1) — […]
(2.1.2) — […]
(2.1.3) — […]
(2.1.4) — Otherwise, if
(2.1.4.1) —
constructible_from<C, Args...>
istrue
, and(2.1.4.2) —
container-appendable<C, range_reference_t<R>>
istrue
:C c(std::forward<Args>(args)...); subrange s{r}; if constexpr (sized_range<R>requires { s.size(); } && reservable-container<C>) c.reserve(static_cast<range_size_t<C>>(ranges::size(r)s.size())); ranges::for_each(rs, container-append(c));