Skip to content

Fix rules for std::vector<bool> - #570

Merged
nunoplopes merged 11 commits into
Cpp2Rust:masterfrom
lucic71:vec-bool-ref
Oct 30, 2026
Merged

nunoplopes merged 11 commits into
Cpp2Rust:masterfrom
lucic71:vec-bool-ref

Conversation

@lucic71

@lucic71 lucic71 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #566

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:

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

@nunoplopes
nunoplopes merged commit ce0915c into Cpp2Rust:master Oct 30, 2026
9 checks passed
@lucic71
lucic71 deleted the vec-bool-ref branch October 30, 2026 21:38
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
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.

Incorrect std::vector<bool>::reference translation.

2 participants