Boost logo

Boost :

Subject: Re: [boost] [concept] concept-based overloading (N2081)
From: Jeffrey Lee Hellrung, Jr. (jhellrung_at_[hidden])
Date: 2010-05-27 13:48:55


On 5/27/2010 10:38 AM, Lorenzo Caminiti wrote:
> Hello all,
>
> How do I use Boost.ConceptCheck and/or Bosot.EnableIf to implement
> concept-based overloading (as discussed in N2081)?
>
> For example, from N2081, the following functions are selected based on
> `Iter` matching one of the two specified concepts:
>
> template<InputIterator Iter> // proposed concept syntax (not C++)
> void advance(Iter& x, Iter::distance_type n) {
> while (n> 0) { ++x; --n; }
> }
>
> template<BidirectionalIterator Iter> // proposed concept syntax (not C++)
> void advance(Iter& x, Iter::distance_type n) {
> if (n> 0) while (n> 0) { ++x; --n; }
> else while (n< 0) { --x; ++n; }
> }
>
> How do I program this in C++ using Boost libraries?
>
> N2081 briefly mentions this can be done in C++ using SFINAE, etc. I
> think I could use `enable_if` if I had some sort of
> `is_input_iterator` metafunction... Please just point me to the
> documentation if this has already been addressed there.
>
> Thank you very much.
>

Do you mean the iterator_category / iterator_traversal metafunctions in
boost/iterator/iterator_traits.hpp ?

http://www.boost.org/libs/iterator/doc/iterator_traits.html

template< class Iterator >
typename boost::disable_if<
     boost::is_convertible<
         typename boost::iterator_traversal< Iterator >::type,
         boost::bidirectional_traversal_tag
>
>::type
advance(...) { ... } // for IncrementableIterator's

template< class Iterator >
typename boost::enable_if<
     boost::is_convertible<
         typename boost::iterator_traversal< Iterator >::type,
         boost::bidirectional_traversal_tag
>
>::type
advance(...) { ... } // for BidirectionalIterator's

I think the primary way to currently effect "concept-based" overloading
is via tags or similar metafunction-based methods. There do exist
introspection techniques, though, such as proto's can_be_called or
is_incrementable in boost/detail/, so it is possible to overload based
on (some) syntactic properties.

Is that what you're looking for?

- Jeff


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