Re: [Boost-bugs] [Boost C++ Libraries] #8011: iterator_range::size() is not SFINAE-friendly

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #8011: iterator_range::size() is not SFINAE-friendly
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-02-12 18:32:46


#8011: iterator_range::size() is not SFINAE-friendly
---------------------------------------------------------+------------------
  Reporter: Jonathan Wakely <jwakely.boost@…> | Owner: neilgroves
      Type: Feature Requests | Status: new
 Milestone: To Be Determined | Component: range
   Version: Boost 1.52.0 | Severity: Problem
Resolution: | Keywords:
---------------------------------------------------------+------------------

Comment (by nathanridge):

 Replying to [comment:2 Jonathan Wakely <jwakely.boost@…>]:
> I have a `zip` function to iterate over a variable number of ranges:
> {{{
> for (auto& e : zip(container, array, other_ranges, ...))
> /* ... */;
> }}}
> In order to ensure iteration stops at the end of the shortest range I
 need to know the length of each range. I used to assume each range had a
 `r.size()` member, but that broke for `iterator_range` with non-random
 access iterators, so now I use `std::distance(std::begin(r),
 std::end(r))`, which is O(n). I would prefer to use `r.size()` if it's
 usable, but I can't use SFINAE to tell if `size()` is usable, because
 `iterator_range<I>::size()` is always defined, but not always usable.
>
> I could define a traits class to detect if the range is `iterator_range`
 and only use `size()` if it doesn't contain RA iterators, but I'd prefer
 to avoid that and just use SFINAE.

 Why not write zip() in such a way that it stops looping when the iterator
 of any one of the ranges reaches end()?

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/8011#comment:3>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:12 UTC