Skip to content

Commit

Permalink
fix: regression in pybind#3271
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Sep 23, 2021
1 parent bf1e50b commit bde20cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
16 changes: 9 additions & 7 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1966,27 +1966,29 @@ struct iterator_state {
};

// Note: these helpers take the iterator by non-const reference because some
// iterators in the wild can't be dereferenced when const.
template <typename Iterator>
// iterators in the wild can't be dereferenced when const. C++ needs the extra parens in decltype
// to enforce an lvalue. The & after Iterator is required for MSVC < 16.9. ResultType cannot be
// reused for result_type due to bugs in ICC, NVCC, and PGI. See #3293.
template <typename Iterator, typename ResultType = decltype((*std::declval<Iterator &>()))>
struct iterator_access {
using result_type = decltype((*std::declval<Iterator>()));
using result_type = decltype((*std::declval<Iterator &>()));
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
result_type operator()(Iterator &it) const {
return *it;
}
};

template <typename Iterator>
template <typename Iterator, typename ResultType = decltype(((*std::declval<Iterator &>()).first)) >
struct iterator_key_access {
using result_type = decltype(((*std::declval<Iterator>()).first));
using result_type = decltype(((*std::declval<Iterator &>()).first));
result_type operator()(Iterator &it) const {
return (*it).first;
}
};

template <typename Iterator>
template <typename Iterator, typename ResultType = decltype(((*std::declval<Iterator &>()).second))>
struct iterator_value_access {
using result_type = decltype(((*std::declval<Iterator>()).second));
using result_type = decltype(((*std::declval<Iterator &>()).second));
result_type operator()(Iterator &it) const {
return (*it).second;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_sequences_and_iterators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,20 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
return py::make_key_iterator(self);
}, py::keep_alive<0, 1>())

// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
.def("make_iterator_keep_alive", [](IntPairs& self) {
return py::make_iterator(self, py::keep_alive<0, 1>());
}, py::keep_alive<0, 1>())
.def("simple_iterator", [](IntPairs& self) {
return py::make_iterator(self);
}, py::keep_alive<0, 1>())
.def("simple_keys", [](IntPairs& self) {
return py::make_key_iterator(self);
}, py::keep_alive<0, 1>())
.def("simple_values", [](IntPairs& self) {
return py::make_value_iterator(self);
}, py::keep_alive<0, 1>())

// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
.def("make_iterator_keep_alive", [](IntPairs& self) {
return py::make_iterator(self, py::keep_alive<0, 1>());
Expand Down
1 change: 1 addition & 0 deletions tests/test_sequences_and_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def test_generalized_iterators_simple():
(0, 5),
]
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]


def test_iterator_referencing():
Expand Down

0 comments on commit bde20cd

Please sign in to comment.