fix issues with std::vector<bool> overload in STL (fixes #216)

This commit is contained in:
Wenzel Jakob 2016-05-30 11:28:21 +02:00
parent dca6b04c5f
commit 5dd33d880d
5 changed files with 19 additions and 5 deletions

View File

@ -29,6 +29,7 @@ void init_ex17(py::module &m) {
.def(pybind11::init<int>()); .def(pybind11::init<int>());
pybind11::bind_vector<unsigned int>(m, "VectorInt"); pybind11::bind_vector<unsigned int>(m, "VectorInt");
pybind11::bind_vector<bool>(m, "VectorBool");
pybind11::bind_vector<El>(m, "VectorEl"); pybind11::bind_vector<El>(m, "VectorEl");

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function
from example import VectorInt, El, VectorEl, VectorVectorEl from example import VectorInt, El, VectorEl, VectorVectorEl, VectorBool
v_int = VectorInt([0, 0]) v_int = VectorInt([0, 0])
print(len(v_int)) print(len(v_int))
@ -38,3 +38,11 @@ vv_a = VectorVectorEl()
vv_a.append(v_a) vv_a.append(v_a)
vv_b = vv_a[0] vv_b = vv_a[0]
print(vv_b) print(vv_b)
vv_c = VectorBool()
for i in range(10):
vv_c.append(i % 2 == 0)
for i in range(10):
if vv_c[i] != (i % 2 == 0):
print("Error!")
print(vv_c)

View File

@ -8,3 +8,4 @@ VectorInt[3, 0, 99, 2, 3]
VectorInt[0, 99, 2, 3] VectorInt[0, 99, 2, 3]
VectorEl[El{1}, El{2}] VectorEl[El{1}, El{2}]
VectorEl[El{1}, El{2}] VectorEl[El{1}, El{2}]
VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]

View File

@ -1019,13 +1019,16 @@ NAMESPACE_END(detail)
template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); } template <typename... Args> detail::init<Args...> init() { return detail::init<Args...>(); }
template <typename Iterator, typename... Extra> iterator make_iterator(Iterator first, Iterator last, Extra&&... extra) { template <typename Iterator,
typename ValueType = decltype(*std::declval<Iterator>()),
typename... Extra>
iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) {
typedef detail::iterator_state<Iterator> state; typedef detail::iterator_state<Iterator> state;
if (!detail::get_type_info(typeid(state))) { if (!detail::get_type_info(typeid(state))) {
class_<state>(handle(), "") class_<state>(handle(), "")
.def("__iter__", [](state &s) -> state& { return s; }) .def("__iter__", [](state &s) -> state& { return s; })
.def("__next__", [](state &s) -> decltype(*std::declval<Iterator>()) { .def("__next__", [](state &s) -> ValueType {
if (s.it == s.end) if (s.it == s.end)
throw stop_iteration(); throw stop_iteration();
return *s.it++; return *s.it++;

View File

@ -136,6 +136,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
using Vector = std::vector<T, Allocator>; using Vector = std::vector<T, Allocator>;
using SizeType = typename Vector::size_type; using SizeType = typename Vector::size_type;
using DiffType = typename Vector::difference_type; using DiffType = typename Vector::difference_type;
using ItType = typename Vector::iterator;
using Class_ = pybind11::class_<Vector, holder_type>; using Class_ = pybind11::class_<Vector, holder_type>;
Class_ cl(m, name.c_str(), std::forward<Args>(args)...); Class_ cl(m, name.c_str(), std::forward<Args>(args)...);
@ -214,7 +215,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
); );
cl.def("__getitem__", cl.def("__getitem__",
[](const Vector &v, SizeType i) { [](const Vector &v, SizeType i) -> T {
if (i >= v.size()) if (i >= v.size())
throw pybind11::index_error(); throw pybind11::index_error();
return v[i]; return v[i];
@ -242,7 +243,7 @@ pybind11::class_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
cl.def("__iter__", cl.def("__iter__",
[](Vector &v) { [](Vector &v) {
return pybind11::make_iterator(v.begin(), v.end()); return pybind11::make_iterator<ItType, T>(v.begin(), v.end());
}, },
pybind11::keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */ pybind11::keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */
); );