[libcxx-commits] [libcxx] [libc++] optimization on ranges::drop_view::begin (#72883) (PR #72929)
via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Nov 22 22:52:26 PST 2023
================
@@ -90,6 +90,10 @@ namespace ranges {
requires (!(__simple_view<_View> &&
random_access_range<const _View> && sized_range<const _View>))
{
+ if constexpr (random_access_range<_View> && sized_range<_View>) {
+ range_difference_t<_View> __dist = ranges::distance(__base_);
----------------
huixie90 wrote:
> > maybe
> > `range_difference_t<_View> __dist = ranges::size(__base_);`
> > is the actual intent of the code?
>
> The standard does not guarantee that the above implicit conversion is well-formed. `ranges::distance` is always preferred over `ranges::size` when deal with iterators.
Only narrowing conversion can be explicit-only. IIUC if size_t -> difeerence_t is narrowing conversion, I think we have other problems. The problem of ranges::distance is that it is not for the purpose of calculating the size in constant time. It is a utility tool to calculate the size in case you cannot caluculate it in constant time, which is the wrong tool here imo. And I strongly against using ‘auto’ when dealing with difference type for the exact reason we are discussing: narrowing conversion.
If the spec does not say size -> difference_t cannot be narrowing, I’d prefer to use either parenthesis or static_cast to do the comversion
https://github.com/llvm/llvm-project/pull/72929
More information about the libcxx-commits
mailing list