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

@@ -1019,13 +1019,16 @@ NAMESPACE_END(detail)
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++;

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 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 @@ pybind11::class_<std::vector<T, Allocator>, 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_<std::vector<T, Allocator>, holder_type> bind_vector(pybind11::m
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 */
);