Boost logo

Boost :

From: Sebastian Theophil (stheophil_at_[hidden])
Date: 2007-11-20 12:53:41


Hello everybody,

1)

I tried to use a simple boost::counting_iterator<size_t> but ran into
the following compiler error:

error C4244: '+=' : conversion from '__int64' to 'size_t', possible loss
of data

void advance(typename super_t::difference_type n)
{
 
BOOST_ITERATOR_ADAPTOR_ASSERT_TRAVERSAL(random_access_traversal_tag)
      m_iterator += n;
}

Obviously, the default difference_type for size_t (and for int by the
way) is __int64 as defined by

template <class T>
struct numeric_difference
{
     typedef typename boost::detail::numeric_traits<T>::difference_type
type;
};

(counting_iterator.hpp:73 called from counting_iterator_base::ctor)

I think I understand why __int64 may be a sensible choice for
calculations with size_t (or int) values. You prevent some overflows
this way.

- Why does the counting_iterator insist on the numeric_traits difference
type? Why allowing an advance operation with an argument possibly larger
than m_iterator itself?

2)

If I manually instantiate the counting_iterator with difference_type
size_t (or int) as boost::counting_iterator<size_t, boost::use_default,
size_t> it will still fail at location

// A policy for wrapped numbers
  template <class Difference, class Incrementable1, class
Incrementable2>
  struct number_distance
  {
      static Difference distance(Incrementable1 x, Incrementable2 y)
      {
          return numeric_distance(x, y);
      }
  };

Instantiated from

template <class OtherIncrementable>
    difference_type
    distance_to(counting_iterator<OtherIncrementable,
CategoryOrTraversal, Difference> const& y) const
    {
      typedef typename mpl::if_<
          detail::is_numeric<Incrementable>
        , detail::number_distance<difference_type, Incrementable,
OtherIncrementable>
        , detail::iterator_distance<difference_type, Incrementable,
OtherIncrementable>
>::type d;

      return d::distance(this->base(), y.base());
    }

With error message

error C4244: 'return' : conversion from '__int64' to '__w64 unsigned
int', possible loss of data.

Again, numeric_distance insists on using __int64 as a difference type
and fails to convert the value properly.

I would greatly appreciate any advice on this because using
counting_iterator would save me a lot of code.

Thanks for everybody's great work,
Sebastian

--
Sebastian Theophil . stheophil_at_[hidden]
Software Engineer
think-cell Software GmbH . Invalidenstr. 34 . 10115 Berlin, Germany 
http://www.think-cell.com . phone +49-30-666473-10 . fax +49-30-666473-19
Geschaftsfuhrer: Dr. Markus Hannebauer, Arno Schoedl . Amtsgericht Charlottenburg, HRB 85229

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk