[libcxx-commits] [PATCH] D131395: [libc++] Implement `lexicographical_compare_three_way`

Adrian Vogelsgesang via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 5 04:47:16 PST 2022


avogelsgesang added inline comments.


================
Comment at: libcxx/include/__algorithm/lexicographical_compare_three_way.h:45
+  using _Len2   = __iter_diff_t<_InputIterator2>;
+  using _Common = common_type_t<_Len1, _Len2>;
+
----------------
huixie90 wrote:
> I would Not use `common_type_t` as it might not exist a `common_type` as `common_type` does not work for types that need implicit conversions.
> The relevant spec here:
> https://eel.is/c++draft/iterator.concept.winc#6.sentence-1
> In short, integral types are only "explicitly" convertible to each other, not implicitly.
> There are related discussion here: https://github.com/ericniebler/range-v3/issues/1745
> 
> I think we want to find which `difference_type` is wider (or we need another type to cover both) and then we need to `static_cast` to that type
I cannot quite follow. Which part of `common_type_t` does not work for this use case?

> I think we want to find which difference_type is wider (or we need another type to cover both)

My understanding is that `common_type_t` does exactly that. Note that https://eel.is/c++draft/iterator.iterators#2.2 guarantees that `difference_type` is a signed integral. As such, there can be no mismatches on signedness, and `common_type_t` should always give the wider `difference_type

> then we need to static_cast to that type

Why do we need static_casts instead of relying on implicit conversions? https://eel.is/c++draft/iterator.concept.winc#6.sentence-1 states that casting to a wider integer type is an implicit conversion.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131395/new/

https://reviews.llvm.org/D131395



More information about the libcxx-commits mailing list