Skip to content

Commit 4b45c4f

Browse files
committed
Forward dereferencing operators in operator_brackets_proxy.
This allows for expressions like it[n]->foo() and (*it[n]).foo() to compile.
1 parent 89d324f commit 4b45c4f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

include/boost/iterator/iterator_facade.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <cstddef>
1111
#include <memory>
12+
#include <utility>
1213
#include <type_traits>
1314

1415
#include <boost/config.hpp>
@@ -370,6 +371,19 @@ class operator_brackets_proxy
370371
return *this;
371372
}
372373

374+
// Provides it[n]->foo(). Leverages chaining of operator->.
375+
reference operator->() const noexcept(noexcept(*m_iter))
376+
{
377+
return *m_iter;
378+
}
379+
380+
// Provides (*it[n]).foo()
381+
template< typename Ref = reference, typename Result = decltype(*std::declval< Ref >()) >
382+
Result operator*() const noexcept(noexcept(**m_iter))
383+
{
384+
return **m_iter;
385+
}
386+
373387
private:
374388
Iterator m_iter;
375389
};

test/iterator_adaptor_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ main()
189189
boost::const_nonconst_iterator_test(i, ++j);
190190
}
191191

192+
// Test that operator_brackets_proxy forwards operator-> and operator*
193+
{
194+
dummyT* ptr_array[] = { array + 0, array + 1, array + 2,
195+
array + 3, array + 4, array + 5 };
196+
197+
ptr_iterator<dummyT*> i(ptr_array);
198+
BOOST_TEST_EQ(i[2]->foo(), 2);
199+
BOOST_TEST_EQ((*i[2]).foo(), 2);
200+
}
201+
192202
int test;
193203
// Test the iterator_traits
194204
{

0 commit comments

Comments
 (0)