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

Tomasz Kaminski tkaminsk@redhat.com
Thu Mar 19 07:41:21 GMT 2026


On Thu, Mar 19, 2026 at 8:35 AM Hewill Kang <hewillk@gmail.com> wrote:

> you should also require that integer_class types are structural
>
> I have already submitted an LWG for this today.
>
>   I think consteval ranges::static_size<Rg>() function would be better
>> that variable, this is not something
>>   we want to emit in binary (because someone took reference to it)
>
>  range_static_size_v basically follows the naming convention of the
> currently available range_size_t.
>
But one is a type and other a value, so I do not think the parallel is
right. You cannot pass range_size_t
 via forwarding reference, and cause additional symbol and 4B of data to be
emitted to binary. And even
if it is not, handling and removing this symbol will increase compile time
cost for no benefit.


>
> inplace_vector could also use mandates on append_range, you cannot append
>> more than capacity
>
>  I don't know how this can be mandates, because the current size of the
> inplace_vector itself is not a constant expression.
>
We can still mandate the same thing as for assign_range, i.e. that
ranges::static_size<RG>() <= N,
not suggesting to check current capacity.


>
> Add some discussion regarding the breaks (not a Precondition to Mandate)
>> that the paper is adding:
>
> Yes, that will be more complete, thank you.
>
> Tomasz Kaminski <tkaminsk@redhat.com> 於 2026年3月19日週四 下午3:18寫道:
>
>>
>>
>> On Wed, Mar 18, 2026 at 5:55 PM Hewill Kang <hewillk@gmail.com> wrote:
>>
>>> I wrote a similar paper, namely
>>> https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3928r0.html
>>> If you want to enhance ref_view::size, then I think ref_view::empty
>>> should also be enhanced, just like the paper did.
>>>
>> Thanks, here are some suggestions:
>> * you should also require that integer_class types are structural
>> * I think consteval ranges::static_size<Rg>() function would be better
>> that variable, this is not something
>>   we want to emit in binary (because someone took reference to it)
>> * inplace_vector could also use mandates on append_range, you cannot
>> append more than capacity
>>
>> Add some discussion regarding the breaks (not a Precondition to Mandate)
>> that the paper is adding:
>> * span CTAD deduces sized range
>> * join_view, join_with_view is now sized sometimes
>>  * split_view
>>
>>>
>>> Tomasz Kamiński <tkaminsk@redhat.com> 於 2026年3月19日週四 上午12:31寫道:
>>>
>>>> If the referenced range is statically sized, instead of referencing a
>>>> pointer whose value is not know at compile time, we return
>>>> integral_constant<range_difference_t<_Rg>, ranges::size(__rg)>::value.
>>>>
>>>> libstdc++-v3/ChangeLog:
>>>>
>>>>         * include/std/ranges (ref_view::size()): Return constant for
>>>> statically
>>>>         sized ranges, even if pointed object is unknown.
>>>>         * testsuite/23_containers/inplace_vector/cons/from_range_neg.cc:
>>>>         Expect errors from ref_view of array examples.
>>>> ---
>>>> v2 uses integral_constant to produce a value of ranges size.
>>>>
>>>>  libstdc++-v3/include/std/ranges                | 18 +++++++++++++++++-
>>>>  .../inplace_vector/cons/from_range_neg.cc      |  6 ++----
>>>>  2 files changed, 19 insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/libstdc++-v3/include/std/ranges
>>>> b/libstdc++-v3/include/std/ranges
>>>> index 95e0109a10c..71ab0d674c9 100644
>>>> --- a/libstdc++-v3/include/std/ranges
>>>> +++ b/libstdc++-v3/include/std/ranges
>>>> @@ -1395,7 +1395,23 @@ 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>)
>>>> +         {
>>>> +           auto __conjure_size_constant
>>>> +             = [](_Range& __rg)
>>>> +               {
>>>> +                 return integral_constant<
>>>> +                          range_difference_t<_Range>,
>>>> +                          ranges::size(__rg)>();
>>>> +               };
>>>> +            return decltype(__conjure_size_constant(*_M_r))::value;
>>>> +         }
>>>> +        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
>>>>
>>>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20260319/43f17586/attachment-0001.htm>


More information about the Libstdc++ mailing list