[PATCH 2/2] libstdc++: Make ref_view<R> statically sized if R is statically sized

Jonathan Wakely jwakely@redhat.com
Wed Mar 18 16:04:11 GMT 2026


On Wed, 18 Mar 2026 at 16:03, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> On Wed, 18 Mar 2026 at 15:16, Tomasz Kamiński <tkaminsk@redhat.com> wrote:
> >
> > If the referenced range is statically sized, instead of referencing a
> > pointer whose value is not know at compile time, we invoke ranges::size
> > on static `__placeholder` variable  of _Range&. The result is then used
> > as initializer of constexpr variable, guaranteeing that evaluation happens
> > at compile time, and __placeholder var is never referenced from runtime.
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/std/ranges (ref_view::_S_placeholder): Declare without
> >         definition.
> >         (ref_view::size()): Use ranges::size on _S_placeholder for
> >         statically sized ranges.
> >         * testsuite/23_containers/inplace_vector/cons/from_range_neg.cc:
> >         Expect errors from ref_view of array examples.
> > --
> > I wonder if that should not be something that is guaranteed in the
> > standsard. I again relly on complete lack of specification when constexpr
> > function are usable at compile time, and I think ref_view<S>::size() can
> > me made one in this case. We could apply the logic (using requires
> > clause) for all ref_view methods in future.
> >
> > I think that this patch is GCC-17 material, as any issues with support
> > of references to uknown, my affect existing code simply recompiled with
> > --std=c++26.
> >
> > Testing on x86_64-linux. OK for GCC-17 when all test passes?
> >
> >  libstdc++-v3/include/std/ranges                     | 13 ++++++++++++-
> >  .../inplace_vector/cons/from_range_neg.cc           |  6 ++----
> >  2 files changed, 14 insertions(+), 5 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> > index 95e0109a10c..e3f28b1ae42 100644
> > --- a/libstdc++-v3/include/std/ranges
> > +++ b/libstdc++-v3/include/std/ranges
> > @@ -1364,6 +1364,7 @@ namespace views::__adaptor
> >      private:
> >        _Range* _M_r;
> >
> > +      static _Range& _S_placeholder;
>
> This concerns me, I think we might get linker errors on some targets,
> and it just feels wrong.
>
> Could we do something else, like:
>
> template<typename R> requires __static_sized_range<R>
> constexpr auto __static_size(R&& r)
> {
>   return std::integral_constant<ranges::range_size_t<R>, ranges::size(r)>{};
> }
>
> and then use decltype(__static_size(declval<R>())) ?

And then use its ::value member, obviously.

I'm not sure if this works though.

>
>
>
>
> >        static void _S_fun(_Range&); // not defined
> >        static void _S_fun(_Range&&) = delete;
> >
> > @@ -1395,7 +1396,17 @@ namespace views::__adaptor
> >
> >        constexpr auto
> >        size() const requires sized_range<_Range>
> > -      { return ranges::size(*_M_r); }
> > +      {
> > +#if __cplusplus > 202302L
> > +       if constexpr (__static_sized_range<_Range>)
> > +         {
> > +           constexpr auto __size = ranges::size(_S_placeholder);
> > +           return __size;
> > +         }
> > +        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_range_neg.cc b/libstdc++-v3/testsuite/23_containers/inplace_vector/cons/from_range_neg.cc
> > index cf87129da51..15fd7b5c285 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
> > @@ -57,14 +57,12 @@ test_all()
> >    test_one(a1); // { dg-error "from here" }
> >    test_one(s1); // { dg-error "from here" }
> >    test_one(s5 | std::views::adjacent<5> | std::views::elements<0>); // { dg-error "from here" }
> > -  // ref_view is not statically sized due pointer dereference
> > -  test_one(a5 | std::views::adjacent<7> | std::views::elements<0>);
> > +  test_one(a5 | 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" }
> >    test_five(s7 | std::views::adjacent<3> | std::views::elements<0>); // { dg-error "from here" }
> > -  // ref_view is not statically sized due pointer dereference
> > -  test_five(a7 | std::views::adjacent<3> | std::views::elements<0>);
> > +  test_five(a7 | std::views::adjacent<3> | std::views::elements<0>); // { dg-error "from here" }
> >  }
> >
> >  // { dg-error "static assertion failed" "" { target *-*-* } 0 }
> > --
> > 2.53.0
> >



More information about the Libstdc++ mailing list