Skip to content

Commit 06a9632

Browse files
committed
fix: extra parens matter?
1 parent ee3de26 commit 06a9632

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
@@ -338,7 +338,17 @@ TEST_SUBMODULE(sequences_and_iterators, m) {
338338
.def("nonzero_values", [](const IntPairs& s) {
339339
return py::make_value_iterator(NonZeroIterator<std::pair<int, int>>(s.begin()), NonZeroSentinel());
340340
}, py::keep_alive<0, 1>())
341-
// test iterator with keep_alive (doesn't work, but tests compile)
341+
.def("simple_iterator", [](IntPairs& self) {
342+
return py::make_iterator(self);
343+
}, py::keep_alive<0, 1>())
344+
.def("simple_keys", [](IntPairs& self) {
345+
return py::make_key_iterator(self);
346+
}, py::keep_alive<0, 1>())
347+
.def("simple_values", [](IntPairs& self) {
348+
return py::make_value_iterator(self);
349+
}, py::keep_alive<0, 1>())
350+
351+
// test iterator with keep_alive (doesn't work so not used at runtime, but tests compile)
342352
.def("make_iterator_keep_alive", [](IntPairs& self) {
343353
return py::make_iterator(self, py::keep_alive<0, 1>());
344354
}, 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
@@ -32,10 +32,6 @@ def test_generalized_iterators():
3232
assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero()) == [(1, 2)]
3333
assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero()) == []
3434

35-
# keep_alive can't be attached
36-
with pytest.raises(RuntimeError):
37-
m.IntPairs([(1, 2), (3, 4), (0, 5)]).make_iterator_keep_alive()
38-
3935
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).nonzero_keys()) == [1, 3]
4036
assert list(m.IntPairs([(1, 2), (2, 0), (0, 3), (4, 5)]).nonzero_keys()) == [1]
4137
assert list(m.IntPairs([(0, 3), (1, 2), (3, 4)]).nonzero_keys()) == []
@@ -56,6 +52,16 @@ def test_generalized_iterators():
5652
next(it)
5753

5854

55+
def test_generalized_iterators_simple():
56+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_iterator()) == [
57+
(1, 2),
58+
(3, 4),
59+
(0, 5),
60+
]
61+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_keys()) == [1, 3, 0]
62+
assert list(m.IntPairs([(1, 2), (3, 4), (0, 5)]).simple_values()) == [2, 4, 5]
63+
64+
5965
def test_iterator_referencing():
6066
"""Test that iterators reference rather than copy their referents."""
6167
vec = m.VectorNonCopyableInt()

0 commit comments

Comments
 (0)