Ticket #4189: transform_iterator.2.patch

File transform_iterator.2.patch, 1.5 KB (added by Daniel Walker, 12 years ago)

use boost::optional to store function objects with non-trivial constructors as suggested by Jeffrey Lee Hellrung

  • boost/iterator/transform_iterator.hpp

     
    1414#include <boost/iterator/iterator_categories.hpp>
    1515#include <boost/mpl/not.hpp>
    1616#include <boost/mpl/bool.hpp>
     17#include <boost/optional.hpp>
    1718#include <boost/type_traits/function_traits.hpp>
     19#include <boost/type_traits/has_trivial_constructor.hpp>
    1820#include <boost/type_traits/is_const.hpp>
    1921#include <boost/type_traits/is_class.hpp>
    2022#include <boost/type_traits/is_function.hpp>
     
    130132   {}
    131133
    132134    UnaryFunc functor() const
    133       { return m_f; }
     135      { return unwrap_functor(m_f); }
    134136
    135137  private:
    136138    typename super_t::reference dereference() const
    137     { return m_f(*this->base()); }
     139    { return unwrap_functor(m_f)(*this->base()); }
    138140
     141    UnaryFunc const& unwrap_functor(UnaryFunc const& t) const
     142    { return t; }
     143
     144    UnaryFunc const& unwrap_functor(optional<UnaryFunc> const& t) const
     145    { return *t; }
     146
     147    typedef typename mpl::if_<
     148        mpl::and_<
     149            is_class<UnaryFunc>
     150          , mpl::not_<has_trivial_constructor<UnaryFunc> >
     151        >
     152      , optional<UnaryFunc>
     153      , UnaryFunc
     154    >::type unary_functor_type;
     155
    139156    // Probably should be the initial base class so it can be
    140157    // optimized away via EBO if it is an empty class.
    141     UnaryFunc m_f;
     158    unary_functor_type m_f;
    142159  };
    143160
    144161  template <class UnaryFunc, class Iterator>