[libc++] Fix ambiguous call in {ranges, std}::find #122641
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes an ambiguous call encountered when using the
std::ranges::find
orstd::find
algorithms withvector<bool>
and a custom-sized allocator.The root cause is that the internal bitwise arithmetic of the
find
algorithms for small integral types exhibits integral promotions, resulting in no single best viable function among the three candidates (__libcpp_ctz(unsigned)
,__libcpp_ctz(unsigned long)
, and__libcpp_ctz(unsigned long long)
), causing an ambiguous call error.To fix this ambiguity, we utilize a dispatcher function
__countr_zero
to directs calls to the right overload of__libcpp_ctz
. Additionally, we have enhanced the internal implementation of__countr_zero
, which wasconstexpr
since C++14 but did not utilizeconstexpr if
for C++17 and later. The improvements include:__countr_zero
constexpr starting from C++11constexpr if
for C++17 and above, while using SFINAE-based overloading for standards prior to C++17.Fixes #122528.