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

Type Caster for std::vector<bool> #255

Closed
maximiliank opened this issue Aug 9, 2023 · 1 comment
Closed

Type Caster for std::vector<bool> #255

maximiliank opened this issue Aug 9, 2023 · 1 comment

Comments

@maximiliank
Copy link
Contributor

The Type Caster for STL std::vector<bool> is not working due to the following range based for loop:

for (auto &value : src) {

See https://stackoverflow.com/a/34085592/9759769.

An example demonstrating the error would be:

#include <nanobind/nanobind.h>
#include <nanobind/stl/vector.h>
#include <vector>

namespace nb = nanobind;

using namespace nb::literals;


// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, misc-use-anonymous-namespace, readability-identifier-length)
NB_MODULE(nanobind_example_ext, m)
{
    m.def("vector_bool", []() { return std::vector<bool>{false, true, false, true}; });
}

which yields on GCC 12.3.0:

[build] /nanobind_example/.venv/lib/python3.11/site-packages/nanobind/include/nanobind/stl/detail/nb_list.h:62:13: error: cannot bind non-const lvalue reference of type ‘std::_Bit_reference&’ to an rvalue of type ‘std::_Bit_iterator::reference’
[build]    62 |             for (auto &value : src) {
[build]       |             ^~~
[build] ninja: build stopped: subcommand failed.
maximiliank added a commit to maximiliank/nanobind that referenced this issue Aug 9, 2023
wjakob pushed a commit that referenced this issue Aug 10, 2023
* Update nb_list.h to allow for std::vector<bool>, see #255

* Added type caster test for std::vector<bool>

* Fixed test name
@wjakob
Copy link
Owner

wjakob commented Aug 10, 2023

Closed via #256.

@wjakob wjakob closed this as completed Aug 10, 2023
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

No branches or pull requests

2 participants