Skip to content

Commit b7338ac

Browse files
committed
fix: extra parens matter?
1 parent 49c04c3 commit b7338ac

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

include/pybind11/pybind11.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,8 +1966,8 @@ struct iterator_state {
19661966
};
19671967

19681968
// Note: these helpers take the iterator by non-const reference because some
1969-
// iterators in the wild can't be dereferenced when const.
1970-
template <typename Iterator, typename ResultType = decltype(*std::declval<Iterator>())>
1969+
// iterators in the wild can't be dereferenced when const. C++ needs the extra parens in decltype.
1970+
template <typename Iterator, typename ResultType = decltype((*std::declval<Iterator>()))>
19711971
struct iterator_access {
19721972
using result_type = ResultType;
19731973
// NOLINTNEXTLINE(readability-const-return-type) // PR #3263
@@ -1976,15 +1976,15 @@ struct iterator_access {
19761976
}
19771977
};
19781978

1979-
template <typename Iterator, typename ResultType = decltype((*std::declval<Iterator>()).first) >
1979+
template <typename Iterator, typename ResultType = decltype(((*std::declval<Iterator>()).first)) >
19801980
struct iterator_key_access {
19811981
using result_type = ResultType;
19821982
result_type operator()(Iterator &it) const {
19831983
return (*it).first;
19841984
}
19851985
};
19861986

1987-
template <typename Iterator, typename ResultType = decltype((*std::declval<Iterator>()).second)>
1987+
template <typename Iterator, typename ResultType = decltype(((*std::declval<Iterator>()).second))>
19881988
struct iterator_value_access {
19891989
using result_type = ResultType;
19901990
result_type operator()(Iterator &it) const {

tests/test_sequences_and_iterators.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
321321
.def("nonzero_values", [](const IntPairs& s) {
322322
return py::make_value_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
323323
}, py::keep_alive<0, 1>())
324-
// test iterator with keep_alive (doesn't work, but tests compile)
324+
.def("simple_iterator", [](IntPairs& self) {
325+
return py::make_iterator(self);
326+
}, py::keep_alive<0, 1>())
327+
.def("simple_keys", [](IntPairs& self) {
328+
return py::make_key_iterator(self);
329+
}, py::keep_alive<0, 1>())
330+
.def("simple_values", [](IntPairs& self) {
331+
return py::make_value_iterator(self);
332+
}, py::keep_alive<0, 1>())
333+
334+
// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
325335
.def("make_iterator_keep_alive", [](IntPairs& self) {
326336
return py::make_iterator(self, py::keep_alive<0, 1>());
327337
}, py::keep_alive<0, 1>())

tests/test_sequences_and_iterators.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ def test_generalized_iterators():
2121
assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
2222
assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
2323

24-
# keep_alive can't be attached
25-
with pytest.raises(RuntimeError):
26-
m.IntPairs([(1, 2), (3, 4), (0, 5)]).make_iterator_keep_alive()
27-
2824
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
2925
assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
3026
assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
@@ -45,6 +41,16 @@ def test_generalized_iterators():
4541
next(it)
4642

4743

44+
def test_generalized_iterators_simple():
45+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
46+
(1, 2),
47+
(3, 4),
48+
(0, 5),
49+
]
50+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
51+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
52+
53+
4854
def test_iterator_referencing():
4955
"""Test that iterators reference rather than copy their referents."""
5056
vec = m.VectorNonCopyableInt()

0 commit comments

Comments
 (0)