Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Fix ambiguous call in {ranges, std}::find #122641

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

winner245
Copy link
Contributor

@winner245 winner245 commented Jan 12, 2025

This PR fixes an ambiguous call encountered when using the std::ranges::find or std::find algorithms with vector<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 was constexpr since C++14 but did not utilize constexpr if for C++17 and later. The improvements include:

  • Making __countr_zero constexpr starting from C++11
  • Eliminating runtime if overhead by employing constexpr if for C++17 and above, while using SFINAE-based overloading for standards prior to C++17.

Fixes #122528.

@winner245 winner245 force-pushed the fix-bitwise-logic-in-find branch 8 times, most recently from 58b3e49 to c4f700f Compare January 12, 2025 20:02
@winner245 winner245 force-pushed the fix-bitwise-logic-in-find branch from c4f700f to 4207592 Compare January 12, 2025 20:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] Ambiguous call encountered in {ranges, std}::{count, find} algorithms
1 participant