diff --git a/example/example17.cpp b/example/example17.cpp index 8ae4cad08..8fd4ad642 100644 --- a/example/example17.cpp +++ b/example/example17.cpp @@ -29,6 +29,7 @@ void init_ex17(py::module &m) { .def(pybind11::init()); pybind11::bind_vector(m, "VectorInt"); + pybind11::bind_vector(m, "VectorBool"); pybind11::bind_vector(m, "VectorEl"); diff --git a/example/example17.py b/example/example17.py index 65e586bcb..feae30708 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 = VectorVectorEl() 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 55e47a68d..cc271f3a9 100644 --- a/example/example17.ref +++ b/example/example17.ref @@ -8,3 +8,4 @@ VectorInt[3, 0, 99, 2, 3] 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 00a7fe0bd..964aea90e 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1019,13 +1019,16 @@ NAMESPACE_END(detail) template detail::init init() { return detail::init(); } -template iterator make_iterator(Iterator first, Iterator last, Extra&&... extra) { +template ()), + typename... Extra> +iterator make_iterator(Iterator first, Iterator last, Extra &&... extra) { typedef detail::iterator_state state; if (!detail::get_type_info(typeid(state))) { class_(handle(), "") .def("__iter__", [](state &s) -> state& { return s; }) - .def("__next__", [](state &s) -> decltype(*std::declval()) { + .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 7de71c8ab..a6b507191 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -136,6 +136,7 @@ pybind11::class_, holder_type> bind_vector(pybind11::m using Vector = std::vector; using SizeType = typename Vector::size_type; using DiffType = typename Vector::difference_type; + using ItType = typename Vector::iterator; using Class_ = pybind11::class_; Class_ cl(m, name.c_str(), std::forward(args)...); @@ -214,7 +215,7 @@ pybind11::class_, holder_type> bind_vector(pybind11::m ); 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 @@ pybind11::class_, holder_type> bind_vector(pybind11::m cl.def("__iter__", [](Vector &v) { - return pybind11::make_iterator(v.begin(), v.end()); + return pybind11::make_iterator(v.begin(), v.end()); }, pybind11::keep_alive<0, 1>() /* Essential: keep list alive while iterator exists */ );