Libstdc++: Make ref_view<R> statically sized if R has static size
Makes `ref_view<R>` statically sized when the underlying range `R` has a static size, avoiding runtime dereferences.
This commit enhances the ref_view in libstdc++ to leverage static size information when available in the underlying range. The change introduces a ranges::__static_size helper function that returns the size of a statically-sized range. This allows ref_view to avoid potentially unsafe dereferences of pointer values at runtime when the size is known at compile time, enabling compile-time optimizations for ranges with static sizes.
In Details
This commit implements part of P3928R0, targeting static_sized_range. ranges::ref_view adapts an existing range without taking ownership. The key is propagating static size information (if available) from the adapted range, enabling contexts like inplace_vector to statically assert sufficient capacity. The interaction with inplace_vector highlights the usability stakes: ensuring ranges derived from static-size arrays or std::array can be efficiently copied into fixed-size buffers.
For Context
In C++, a 'range' is a sequence of elements that can be iterated over. ref_view provides a way to view an existing range without copying its elements. 'Statically sized' ranges have sizes known at compile time, like fixed-size arrays. This commit optimizes ref_view for statically-sized ranges, allowing the compiler to perform more optimizations and prevent runtime errors by ensuring that operations like copying into fixed-size containers can be checked at compile time.