fix issues with std::vector<bool> overload in STL (fixes #216)
diff --git a/example/example17.cpp b/example/example17.cpp
index 8ae4cad..8fd4ad6 100644
--- a/example/example17.cpp
+++ b/example/example17.cpp
@@ -29,6 +29,7 @@
.def(pybind11::init<int>());
pybind11::bind_vector<unsigned int>(m, "VectorInt");
+ pybind11::bind_vector<bool>(m, "VectorBool");
pybind11::bind_vector<El>(m, "VectorEl");
diff --git a/example/example17.py b/example/example17.py
index 65e586b..feae307 100644
--- a/example/example17.py
+++ b/example/example17.py
@@ -1,7 +1,7 @@
#!/usr/bin/env python
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])
print(len(v_int))
@@ -38,3 +38,11 @@
vv_a.append(v_a)
vv_b = vv_a[0]
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)
diff --git a/example/example17.ref b/example/example17.ref
index 55e47a6..cc271f3 100644
--- a/example/example17.ref
+++ b/example/example17.ref
@@ -8,3 +8,4 @@
VectorInt[0, 99, 2, 3]
VectorEl[El{1}, El{2}]
VectorEl[El{1}, El{2}]
+VectorBool[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index 00a7fe0..964aea9 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -1019,13 +1019,16 @@
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;
if (!detail::get_type_info(typeid(state))) {
class_<state>(handle(), "")
.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)
throw stop_iteration();
return *s.it++;
diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h
index 7de71c8..a6b5071 100644
--- a/include/pybind11/stl_bind.h
+++ b/include/pybind11/stl_bind.h
@@ -136,6 +136,7 @@
using Vector = std::vector<T, Allocator>;
using SizeType = typename Vector::size_type;
using DiffType = typename Vector::difference_type;
+ using ItType = typename Vector::iterator;
using Class_ = pybind11::class_<Vector, holder_type>;
Class_ cl(m, name.c_str(), std::forward<Args>(args)...);
@@ -214,7 +215,7 @@
);
cl.def("__getitem__",
- [](const Vector &v, SizeType i) {
+ [](const Vector &v, SizeType i) -> T {
if (i >= v.size())
throw pybind11::index_error();
return v[i];
@@ -242,7 +243,7 @@
cl.def("__iter__",
[](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 */
);