views::as_const
on ref_view<T>
should return ref_view<const T>
Section: 25.7.22.1 [range.as.const.overview] Status: C++23 Submitter: Tomasz Kamiński Opened: 2022-11-03 Last modified: 2023-11-22
Priority: Not Prioritized
View all other issues in [range.as.const.overview].
View all issues with C++23 status.
Discussion:
For v
being a non-const lvalue of type std::vector<int>
, views::as_const(v)
produces ref_view<std::vector<int> const>
. However, when v
is converted to
ref_view
by using views::all
, views::as_const(views::all(v))
produces
as_const_view<ref_view<std::vector<int>>>
.
views::as_const
on ref_view<T>
should produce ref_view<const T>
when
const T
models a constant range. This will reduce the number of instantiations, and make a behavior
of views::as_const
consistent on references and ref_view
to containers.
[Kona 2022-11-08; Move to Ready]
[2023-02-13 Approved at February 2023 meeting in Issaquah. Status changed: Voting → WP.]
Proposed resolution:
This wording is relative to N4917.
Modify 25.7.22.1 [range.as.const.overview] as indicated:
[Drafting note: If we have
ref_view<V>
, whenV
is constant propagating view (single_view
,owning_view
), we still can (and should) produceref_view<V const>
. This wording achieves that.]
-2- The name
views::as_const
denotes a range adaptor object (25.7.2 [range.adaptor.object]). LetE
be an expression, letT
bedecltype((E))
, and letU
beremove_cvref_t<T>
. The expressionviews::as_const(E)
is expression-equivalent to:
(2.1) — If
views::all_t<T>
modelsconstant_range
, thenviews::all(E)
.(2.2) — Otherwise, if
U
denotesspan<X, Extent>
for some typeX
and some extentExtent
, thenspan<const X, Extent>(E)
.(2.?) — Otherwise, if
U
denotesref_view<X>
for some typeX
andconst X
modelsconstant_range
, thenref_view(static_cast<const X&>(E.base()))
.(2.3) — Otherwise, if
E
is an lvalue,const U
modelsconstant_range
, andU
does not modelview
, thenref_view(static_cast<const U&>(E))
.(2.4) — Otherwise,
as_const_view(E)
.