Boost logo

Boost :

Subject: Re: [boost] [Review:Algorithms] Order of args to clamp
From: Christian Holmquist (c.holmquist_at_[hidden])
Date: 2011-09-26 14:00:57


On 26 September 2011 12:40, Marshall Clow <mclow.lists_at_[hidden]> wrote:

> On Sep 26, 2011, at 9:33 AM, Dave Abrahams wrote:
>
> >
> > on Mon Sep 26 2011, Andrew Sutton <asutton.list-AT-gmail.com> wrote:
> >
> >>> It seems to me that a clamp_range is just:
> >>>
> >>> std::transform ( begin, end, out, clamp ( _1, lo, hi ))
> >>>
> >>> is that what you meant?
> >>
> >> Sorry... this got lost in the thread. That looks like the right
> >> implementation, but it might be nice to provide an interface for it:
> >>
> >> template<typename Iter, typename T>
> >> void clamp_range(Iter first, Iter last, cont T& high, const T& low)
> >
> > IMO it clearly should be:
> >
> > template<typename Iter>
> > void clamp_range(
> > Iter first, Iter last,
> > typename itertor_traits<Iter>::value_type const& high,
> > typename itertor_traits<Iter>::value_type const& low)
>
>
> BTW, clamp_range defined like this fails on the following code:
>
> int inputs [] = { 0, 1, 2, 3, 4 };
> clamp_range ( &inputs[0], &inputs[5], 2, 4 );
>
> because pointers are not iterators.
>

pointers should have a specialization of iterator_traits.
The following compiles for me

template<typename Iter>
void clamp_range(Iter first, Iter last,
 typename std::iterator_traits<Iter>::value_type const& high,
 typename std::iterator_traits<Iter>::value_type const& low)
{
for(; first != last; ++first)
{
                ....
}
}

void main()
{
int inputs [] = { 0, 1, 2, 3, 4 };
clamp_range ( &inputs[0], &inputs[5], 2, 4 );
}


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