Skip to content

Commit 7c77e52

Browse files
committed
Always use operator_brackets_proxy in iterator_facade.
This avoids returning a copy of the value from operator[], which can be unexpected by users wanting to obtain an address of the value referenced by the iterator. We still want to return a proxy instead of the iterator's reference since the iterator may be holding the value internally, and the iterator gets destroyed after returning from operator[], so the returned reference would be dangling. Closes #61.
1 parent 256004a commit 7c77e52

1 file changed

Lines changed: 2 additions & 40 deletions

File tree

include/boost/iterator/iterator_facade.hpp

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -368,40 +368,6 @@ class operator_brackets_proxy
368368
Iterator m_iter;
369369
};
370370

371-
// A metafunction that determines whether operator[] must return a
372-
// proxy, or whether it can simply return a copy of the value_type.
373-
template< typename ValueType, typename Reference >
374-
struct use_operator_brackets_proxy :
375-
public detail::negation<
376-
detail::conjunction<
377-
std::is_trivially_copyable< ValueType >,
378-
iterator_writability_disabled< ValueType, Reference >
379-
>
380-
>
381-
{};
382-
383-
template< typename Iterator, typename Value, typename Reference >
384-
struct operator_brackets_result
385-
{
386-
using type = typename std::conditional<
387-
use_operator_brackets_proxy< Value, Reference >::value,
388-
operator_brackets_proxy< Iterator >,
389-
Value
390-
>::type;
391-
};
392-
393-
template< typename Iterator >
394-
inline operator_brackets_proxy< Iterator > make_operator_brackets_result(Iterator const& iter, std::true_type)
395-
{
396-
return operator_brackets_proxy< Iterator >(iter);
397-
}
398-
399-
template< typename Iterator >
400-
inline typename Iterator::value_type make_operator_brackets_result(Iterator const& iter, std::false_type)
401-
{
402-
return *iter;
403-
}
404-
405371
// A binary metafunction class that always returns bool.
406372
template< typename Iterator1, typename Iterator2 >
407373
using always_bool_t = bool;
@@ -678,13 +644,9 @@ class iterator_facade_base< Derived, Value, CategoryOrTraversal, Reference, Diff
678644
using difference_type = typename base_type::difference_type;
679645

680646
public:
681-
typename boost::iterators::detail::operator_brackets_result< Derived, Value, reference >::type
682-
operator[](difference_type n) const
647+
operator_brackets_proxy< Derived > operator[](difference_type n) const
683648
{
684-
return boost::iterators::detail::make_operator_brackets_result< Derived >(
685-
this->derived() + n,
686-
std::integral_constant< bool, boost::iterators::detail::use_operator_brackets_proxy< Value, Reference >::value >{}
687-
);
649+
return operator_brackets_proxy< Derived >(this->derived() + n);
688650
}
689651

690652
Derived& operator+=(difference_type n)

0 commit comments

Comments
 (0)