[PATCH v2 4/4] libstdc++: Make ref_view<R> statically sized if R has static size
Tomasz Kaminski
tkaminsk@redhat.com
Thu May 21 12:15:53 GMT 2026
On Thu, May 21, 2026 at 2:11 PM Patrick Palka <ppalka@redhat.com> wrote:
> On Wed, 20 May 2026, Tomasz Kaminski wrote:
>
> >
> >
> > On Wed, May 20, 2026 at 12:04 AM Patrick Palka <ppalka@redhat.com>
> wrote:
> > On Fri, 24 Apr 2026, Tomasz Kamiński wrote:
> >
> > > This patch introduces ranges::__static_size<_Range> helper
> functions,
> > > that returns ranges::size(__rg) for __statically_sized_range.
> > > This function is then used for ref_view<R>::size if R has static
> size,
> > > avoiding derefence of pointer value that is not know at compile
> time.
> > > Similary for ref_view<R>::empty() we compare the size with zero,
> > > if it is know statically.
> > >
> > > This implements revelant part of P3928R0: static_sized_range by
> Hewill Kang.
> > > As standard does not specify when constexpr functions are usable
> at compile
> > > time, such implementation are allowed (but not mandated) by
> current draft.
> > >
> > > libstdc++-v3/ChangeLog:
> > >
> > > * include/bits/ranges_base.h (ranges::__static_size):
> Define.
> > > * include/std/ranges (ref_view::size()): For ranges with
> static
> > > size return ranges::__static_size<_Range> == 0.
> > > (ref_view::empty): For ranges with static size, compare it
> > > against zero.
> > > *
> testsuite/23_containers/inplace_vector/cons/from_iota_neg.cc:
> > > Expect errors from ref_view uses.
> > > *
> testsuite/23_containers/inplace_vector/cons/from_range_neg.cc:
> > > Expect errors from ref_view uses.
> > > ---
> > > v2 also expands the empty() method, and adds __static_size
> helper funciton.
> > > We could really use some consteval __builtin_unknown_ref(_Tp),
> that will
> > > produce reference to unknown object of type _Tp. This will avoid
> nested
> > > lambda and instantiating integer_constant.
> > >
> > > Tested on x86_64-linux. OK for trunk?
> > >
> > > libstdc++-v3/include/bits/ranges_base.h | 9 +++++++++
> > > libstdc++-v3/include/std/ranges | 18
> ++++++++++++++++--
> > > .../inplace_vector/cons/from_iota_neg.cc | 6 ++----
> > > .../inplace_vector/cons/from_range_neg.cc | 10 ++++------
> > > 4 files changed, 31 insertions(+), 12 deletions(-)
> > >
> > > diff --git a/libstdc++-v3/include/bits/ranges_base.h
> b/libstdc++-v3/include/bits/ranges_base.h
> > > index fea4100eb09..e61ef75f857 100644
> > > --- a/libstdc++-v3/include/bits/ranges_base.h
> > > +++ b/libstdc++-v3/include/bits/ranges_base.h
> > > @@ -562,6 +562,15 @@ namespace ranges
> > > template<typename _Tp>
> > > concept __static_sized_range = sized_range<_Tp> && requires
> (_Tp& __t)
> > > { static_cast<char(*)[size_t(ranges::size(__t) >=
> 0)]>(nullptr); };
> > > +
> > > + template<__static_sized_range _Tp>
> >
> > _Range not _Tp
> >
> > > + consteval range_size_t<_Tp>
> > > + __static_size()
> > > + {
> > > + auto __conjure = [](_Tp& __t)
> >
> > __r not __t
> >
> > > + { return integral_constant<range_size_t<_Tp>,
> ranges::size(__t)>{}; };
> > > + return decltype(__conjure(std::declval<_Tp&>()))::value;
> > > + }
> >
> > Maybe we could fix the first parameter of integral_constant to
> size_t and
> > cast its value to range_size_t afterwards, which should result in
> fewer
> > integral_constant instantiations.
> >
> > I was hoping I would get a building so I could avoid all of this,
> created
> > ticket: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125394
> >
> > I need to be careful about ranges with size greater than size_t, but can
> > workaround that. Will post new patch soon.
> >
> >
> > It might make sense to add another overload that takes a _Range&
> > parameter to allow the range type to be deduced. And that overload
> > could be implemented as a one liner return ranges::size(__r).
> > We could then define <simd>'s __static_range_size in terms of this.
> >
> > What is the use case you are thinking of? Because if you have _Range&
> > object that you can pass, you could invoke ranges::size(__r) directly.
> > So unless, I am missing something that sounds like extra indirection,
> > that could be avoided.
>
> True.. So I guess it doesn't matter that ranges::size is only constexppr
> not consteval because of the escalalation stuff? Never mind then.
>
I was thinking about uses inside static_assert, if contexpr conditions or
requires clauses, for which it needs to be core constant expressions.
To ensure it is evaluated at compile time, I simply did:
constexpr auto x = ranges::size(__r);
>
> >
> >
> > > #endif // C++26
> > >
> > > template<typename _Derived>
> > > diff --git a/libstdc++-v3/include/std/ranges
> b/libstdc++-v3/include/std/ranges
> > > index 4f2082487d1..c1e8ac3cb17 100644
> > > --- a/libstdc++-v3/include/std/ranges
> > > +++ b/libstdc++-v3/include/std/ranges
> > > @@ -1380,11 +1380,25 @@ namespace views::__adaptor
> > >
> > > constexpr bool
> > > empty() const requires requires { ranges::empty(*_M_r); }
> > > - { return ranges::empty(*_M_r); }
> > > + {
> > > +#if __cplusplus > 202302L
> > > + if constexpr (__static_sized_range<_Range>)
> > > + return ranges::__static_size<_Range>() == 0;
> > > + else
> > > +#endif
> > > + return ranges::empty(*_M_r);
> > > + }
> > >
> > > constexpr auto
> > > size() const requires sized_range<_Range>
> > > - { return ranges::size(*_M_r); }
> > > + {
> > > +#if __cplusplus > 202302L
> > > + if constexpr (__static_sized_range<_Range>)
> > > + return ranges::__static_size<_Range>();
> > > + else
> > > +#endif
> > > + return ranges::size(*_M_r);
> > > + }
> > >
> > > constexpr auto
> > > data() const requires contiguous_range<_Range>
> > > diff --git
> a/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_iota_neg.cc
> b/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_iota_neg.cc
> > > index ae0b96175b7..6b66b5d97ed 100644
> > > ---
> a/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_iota_neg.cc
> > > +++
> b/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_iota_neg.cc
> > > @@ -33,14 +33,12 @@ test_all()
> > > std::inplace_vector<int, 15> tr1(std::from_range,
> ref_view(m12));
> > >
> > > std::inplace_vector<int, 10> tm2(std::from_range, m12); // {
> dg-error "(from here|expansion of)" }
> > > - // ref_view is not statically sized due pointer dereference
> > > - std::inplace_vector<int, 10> tr2(std::from_range,
> ref_view(m12));
> > > + std::inplace_vector<int, 10> tr2(std::from_range,
> ref_view(m12)); // { dg-error "(from here|expansion of)" }
> > >
> > > StaticIota<__int128, 0> mm;
> > >
> > > std::inplace_vector<int, 10> tm3(std::from_range, mm); // {
> dg-error "(from here|expansion of)" }
> > > - // ref_view is not statically sized due pointer dereference
> > > - std::inplace_vector<int, 10> tr3(std::from_range,
> ref_view(mm));
> > > + std::inplace_vector<int, 10> tr3(std::from_range,
> ref_view(mm)); // { dg-error "(from here|expansion of)" }
> > > }
> > >
> > > // { dg-error "static assertion failed" "" { target *-*-* } 0 }
> > > diff --git
> a/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_range_neg.cc
> b/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_range_neg.cc
> > > index 48d5b4c56f5..ea187761e5e 100644
> > > ---
> a/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_range_neg.cc
> > > +++
> b/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_range_neg.cc
> > > @@ -62,16 +62,14 @@ test_all()
> > >
> > > test_one(a1); // { dg-error "from here" }
> > > test_one(s1); // { dg-error "from here" }
> > > - // ref_view is not statically sized due pointer dereference
> > > - test_one(ref_view(a1));
> > > - test_one(a5 | std::views::adjacent<7> |
> std::views::elements<0>);
> > > + test_one(ref_view(a1)); // { dg-error "from here" }
> > > + test_one(a5 | std::views::adjacent<5> |
> std::views::elements<0>); // { dg-error "from here" }
> > > test_one(s5 | std::views::adjacent<5> |
> std::views::elements<0>); // { dg-error "from here" }
> > >
> > > test_five(a5); // { dg-error "from here" }
> > > test_five(s5); // { dg-error "from here" }
> > > - // ref_view is not statically sized due pointer dereference
> > > - test_five(ref_view(a5));
> > > - test_five(a7 | std::views::adjacent<3> |
> std::views::elements<0>);
> > > + test_five(ref_view(a5)); // { dg-error "from here" }
> > > + test_five(a7 | std::views::adjacent<3> |
> std::views::elements<0>); // { dg-error "from here" }
> > > test_five(s7 | std::views::adjacent<3> |
> std::views::elements<0>); // { dg-error "from here" }
> > > }
> > >
> > > --
> > > 2.53.0
> > >
> > >
> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260521/c2ee71bd/attachment-0001.htm>
More information about the Libstdc++
mailing list