- BREAKING CHANGE: iterator_facade::pointer now corresponds to the actual result of iterator_facade::operator-> rather than Value*. This required an adjustment to a test.

- The logic for determining the result of iterator_facade::operator[] has been factored out into a separate detail header in preparation for its potential use in iterator_range to avoid iterator_range::operator[] from returning a reference to a temporary.

[SVN r80901]
This commit is contained in:
Jeffrey Lee Hellrung, Jr 2012-10-08 02:02:09 +00:00 committed by Peter Dimov
parent 2e099caceb
commit d291c7b43e
3 changed files with 130 additions and 111 deletions

View File

@ -0,0 +1,88 @@
// (C) Copyright David Abrahams 2002.
// (C) Copyright Jeremy Siek 2002.
// (C) Copyright Thomas Witt 2002.
// (C) Copyright Jeffrey Lee Hellrung, Jr. 2012.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef BOOST_OPERATOR_BRACKETS_DISPATCH_07102012JLH_HPP
#define BOOST_OPERATOR_BRACKETS_DISPATCH_07102012JLH_HPP
#include <boost/iterator/detail/facade_iterator_category.hpp>
#include <boost/type_traits/is_pod.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/mpl/if.hpp>
namespace boost { namespace detail {
// operator[] must return a proxy in case iterator destruction invalidates
// referents.
// To see why, consider the following implementation of operator[]:
// reference operator[](difference_type n) const
// { return *(*this + n); }
// The problem here is that operator[] would return a reference created from
// a temporary iterator.
template <class Value>
struct operator_brackets_value
{
typedef Value result_type;
template <class Iterator>
static result_type apply(Iterator const & i)
{ return *i; }
};
template <class Iterator, class Reference>
struct operator_brackets_const_proxy
{
class result_type
{
Iterator const m_i;
explicit result_type(Iterator const & i) : m_i(i) { }
friend struct operator_brackets_const_proxy;
void operator=(result_type&);
public:
operator Reference() const { return *m_i; }
};
static result_type apply(Iterator const & i)
{ return result_type(i); }
};
template <class Iterator, class Reference>
struct operator_brackets_proxy
{
class result_type
{
Iterator const m_i;
explicit result_type(Iterator const & i) : m_i(i) { }
friend struct operator_brackets_proxy;
void operator=(result_type&);
public:
operator Reference() const { return *m_i; }
operator_brackets_proxy const & operator=(
typename Iterator::value_type const & x) const
{ *m_i = x; return *this; }
};
static result_type apply(Iterator const & i)
{ return result_type(i); }
};
template <class Iterator, class ValueType, class Reference>
struct operator_brackets_dispatch
{
typedef typename mpl::if_c<
iterator_writability_disabled<ValueType,Reference>::value,
typename mpl::if_c<
boost::is_POD<ValueType>::value,
operator_brackets_value<typename boost::remove_const<ValueType>::type>,
operator_brackets_const_proxy<Iterator,Reference>
>::type,
operator_brackets_proxy<Iterator,Reference>
>::type type;
};
} } // namespace detail / namespace boost
#endif // #ifndef BOOST_OPERATOR_BRACKETS_DISPATCH_07102012JLH_HPP

View File

@ -1,6 +1,7 @@
// (C) Copyright David Abrahams 2002. // (C) Copyright David Abrahams 2002.
// (C) Copyright Jeremy Siek 2002. // (C) Copyright Jeremy Siek 2002.
// (C) Copyright Thomas Witt 2002. // (C) Copyright Thomas Witt 2002.
// (C) copyright Jeffrey Lee Hellrung, Jr. 2012.
// Distributed under the Boost Software License, Version 1.0. (See // Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at // accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt) // http://www.boost.org/LICENSE_1_0.txt)
@ -13,6 +14,7 @@
#include <boost/iterator/detail/facade_iterator_category.hpp> #include <boost/iterator/detail/facade_iterator_category.hpp>
#include <boost/iterator/detail/enable_if.hpp> #include <boost/iterator/detail/enable_if.hpp>
#include <boost/iterator/detail/operator_brackets_dispatch.hpp>
#include <boost/static_assert.hpp> #include <boost/static_assert.hpp>
#include <boost/utility/addressof.hpp> #include <boost/utility/addressof.hpp>
@ -296,7 +298,7 @@ namespace boost
// standard's requirements. If *i is not a reference type, we must still // standard's requirements. If *i is not a reference type, we must still
// produce an lvalue to which a pointer can be formed. We do that by // produce an lvalue to which a pointer can be formed. We do that by
// returning a proxy object containing an instance of the reference object. // returning a proxy object containing an instance of the reference object.
template <class Reference, class Pointer> template <class Reference>
struct operator_arrow_dispatch // proxy references struct operator_arrow_dispatch // proxy references
{ {
struct proxy struct proxy
@ -315,10 +317,10 @@ namespace boost
} }
}; };
template <class T, class Pointer> template <class T>
struct operator_arrow_dispatch<T&, Pointer> // "real" references struct operator_arrow_dispatch<T&> // "real" references
{ {
typedef Pointer result_type; typedef T* result_type;
static result_type apply(T& x) static result_type apply(T& x)
{ {
return boost::addressof(x); return boost::addressof(x);
@ -328,79 +330,12 @@ namespace boost
# if BOOST_WORKAROUND(BOOST_MSVC, < 1300) # if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
// Deal with ETI // Deal with ETI
template<> template<>
struct operator_arrow_dispatch<int, int> struct operator_arrow_dispatch<int>
{ {
typedef int result_type; typedef int result_type;
}; };
# endif # endif
// A proxy return type for operator[], needed to deal with
// iterators that may invalidate referents upon destruction.
// Consider the temporary iterator in *(a + n)
template <class Iterator>
class operator_brackets_proxy
{
// Iterator is actually an iterator_facade, so we do not have to
// go through iterator_traits to access the traits.
typedef typename Iterator::reference reference;
typedef typename Iterator::value_type value_type;
public:
operator_brackets_proxy(Iterator const& iter)
: m_iter(iter)
{}
operator reference() const
{
return *m_iter;
}
operator_brackets_proxy& operator=(value_type const& val)
{
*m_iter = val;
return *this;
}
private:
Iterator m_iter;
};
// A metafunction that determines whether operator[] must return a
// proxy, or whether it can simply return a copy of the value_type.
template <class ValueType, class Reference>
struct use_operator_brackets_proxy
: mpl::not_<
mpl::and_<
// Really we want an is_copy_constructible trait here,
// but is_POD will have to suffice in the meantime.
boost::is_POD<ValueType>
, iterator_writability_disabled<ValueType,Reference>
>
>
{};
template <class Iterator, class Value, class Reference>
struct operator_brackets_result
{
typedef typename mpl::if_<
use_operator_brackets_proxy<Value,Reference>
, operator_brackets_proxy<Iterator>
, Value
>::type type;
};
template <class Iterator>
operator_brackets_proxy<Iterator> make_operator_brackets_result(Iterator const& iter, mpl::true_)
{
return operator_brackets_proxy<Iterator>(iter);
}
template <class Iterator>
typename Iterator::value_type make_operator_brackets_result(Iterator const& iter, mpl::false_)
{
return *iter;
}
struct choose_difference_type struct choose_difference_type
{ {
template <class I1, class I2> template <class I1, class I2>
@ -616,9 +551,10 @@ namespace boost
> associated_types; > associated_types;
typedef boost::detail::operator_arrow_dispatch< typedef boost::detail::operator_arrow_dispatch<
Reference Reference> operator_arrow_dispatch_;
, typename associated_types::pointer
> operator_arrow_dispatch_; typedef typename boost::detail::operator_brackets_dispatch<
Derived, Value, Reference>::type operator_brackets_dispatch_;
protected: protected:
// For use by derived classes // For use by derived classes
@ -644,15 +580,10 @@ namespace boost
return operator_arrow_dispatch_::apply(*this->derived()); return operator_arrow_dispatch_::apply(*this->derived());
} }
typename boost::detail::operator_brackets_result<Derived,Value,reference>::type typename operator_brackets_dispatch_::result_type
operator[](difference_type n) const operator[](difference_type n) const
{ {
typedef boost::detail::use_operator_brackets_proxy<Value,Reference> use_proxy; return operator_brackets_dispatch_::apply(this->derived() + n);
return boost::detail::make_operator_brackets_result<Derived>(
this->derived() + n
, use_proxy()
);
} }
Derived& operator++() Derived& operator++()

View File

@ -82,7 +82,7 @@ int main()
typedef boost::indirect_iterator<char**, int, std::random_access_iterator_tag, long&, short> Iter; typedef boost::indirect_iterator<char**, int, std::random_access_iterator_tag, long&, short> Iter;
STATIC_ASSERT_SAME(Iter::value_type, int); STATIC_ASSERT_SAME(Iter::value_type, int);
STATIC_ASSERT_SAME(Iter::reference, long&); STATIC_ASSERT_SAME(Iter::reference, long&);
STATIC_ASSERT_SAME(Iter::pointer, int*); STATIC_ASSERT_SAME(Iter::pointer, long*);
STATIC_ASSERT_SAME(Iter::difference_type, short); STATIC_ASSERT_SAME(Iter::difference_type, short);
} }
return 0; return 0;