Repository navigation
Fix rules for std::vector<bool> - #570
Merged
Merged
Conversation
joaotgouveia
pushed a commit
to joaotgouveia/cpp2rust
that referenced
this pull request
Oct 30, 2026
Fixes Cpp2Rust#556 Adds rules for `std::vector<bool>::reference` and `std::vector<bool>::iterator::reference`. The reference of vector bool is not a real C++ `&`, instead it's a proxy type that acts as a reference. GetDeclaredReturnType and IgnoreParenNoopCasts take care of handling this. Then, because in the following rule T1 lives in an undeduced context, C++ cannot use overload resolution to resolve the type: ```cpp template <typename T1> typename std::vector<bool, T1>::reference f118(std::vector<bool, T1> &a0, std::size_t a1) { return a0.operator[](a1); } ``` Instead we use two techniques to resolve T1: 1. Use the spelled name of the usage. For `std::vector<bool>::reference r1 = v1[0]` the spelled name is `std::vector<bool>::reference`. `reference` is a member of the owner `std::vector<bool, alllocator<bool>>` so T1 becomes `allocator<bool>` 2. For `std::vector<bool>::iterator::reference` the owner is `std::vector<bool>::iteartor` which is not the same as `std::vector<bool, allocator<bool>>` which gives T1, so hop through owners until we reach the correct one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #566
Adds rules for
std::vector<bool>::referenceandstd::vector<bool>::iterator::reference.The reference of vector bool is not a real C++
&, instead it's a proxy type that acts as a reference. GetDeclaredReturnType and IgnoreParenNoopCasts take care of handling this.Then, because in the following rule T1 lives in an undeduced context, C++ cannot use overload resolution to resolve the type:
Instead we use two techniques to resolve T1:
std::vector<bool>::reference r1 = v1[0]the spelled name isstd::vector<bool>::reference.referenceis a member of the ownerstd::vector<bool, alllocator<bool>>so T1 becomesallocator<bool>std::vector<bool>::iterator::referencethe owner isstd::vector<bool>::iteartorwhich is not the same asstd::vector<bool, allocator<bool>>which gives T1, so hop through owners until we reach the correct one