Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2005-02-21 10:40:11


Aleksey Gurtovoy <agurtovoy_at_[hidden]> writes:

> Writing an algorithm for _bidirectional_ iterators and making sure
> that it gets selected for random-access ones as well is somewhat more
> tricky:
>
> template< typename Iterator > struct algorithm
> : if_c<
> iterator_category<Iterator>::type::value
> >= bidirectional_iterator_tag::value
> , bidirectional_algorithm<Iterator>
> , forward_algorithm<Iterator>
> >
> {
> };
>
> but try to write this up assuming that _there is_ an implicit
> conversion between 'random_access_iterator_tag' and
> 'bidirectional_iterator_tag', and you'll see that you'll end up with
> something at least as complex as the above.

If there was a _form_ relationship among MPL iterator tags just like
the _inheritance_ relationship among the STL ones, it would be easier
to do this with partial specialization. E.g.,

  typedef tag<> forward_iterator_tag;
  typedef tag<tag<> > bidirectional_iterator_tag;
  typedef tag<tag<tag<> > > random_access_iterator_tag;

Unfortunately, I don't know how to make this look right. A
specialization on:

  template <class T, class Category>
  struct algorithm<
        T
      , tag<tag<Category> > // Bidirectional
>
  {
    ...
  };

doesn't really communicate well. Maybe some built-in SFINAE
facilities would help?

  template <class Iterator, class Enable = void>
  struct algorithm;

  template<class Iterator>
  struct algorithm<Iterator, typename bidirectional_iterator_tag::enable_if<Iterator>::type>
  {
    ...
  };

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net