Skip to content

Commit e968c33

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 da00617 commit e968c33

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

include/boost/iterator/iterator_facade.hpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,25 @@ class operator_brackets_proxy
365365
typename std::remove_cv< typename std::remove_reference< T >::type >::type
366366
>::value,
367367
operator_brackets_proxy&
368-
>::type operator= (T&& val) noexcept(std::is_nothrow_assignable< reference, T&& >::value)
368+
>::type operator= (T&& val) noexcept(noexcept(*std::declval< Iterator const& >() = std::declval< T&& >()))
369369
{
370370
*m_iter = static_cast< T&& >(val);
371371
return *this;
372372
}
373373

374+
// Provides it[n]->foo(). Leverages chaining of operator->.
375+
reference operator->() const noexcept(noexcept(*std::declval< Iterator const& >()))
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(**std::declval< Iterator const& >()))
383+
{
384+
return **m_iter;
385+
}
386+
374387
private:
375388
Iterator m_iter;
376389
};

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)