Boost logo

Boost :

From: Sean Parent (sparent_at_[hidden])
Date: 2001-10-31 12:47:30


on 10/30/01 7:05 AM, williamkempf_at_[hidden] at williamkempf_at_[hidden]
wrote:

>>> I think copy should look more like:
>>>
>>> template <typename InputRange, typename OutputIterator>
>>> OutputIterator copy( InputRange src, OutputIterator dst ) {
>>> for ( ; !src.empty(); ++src)
>>> *dst = *src;
>>> }
>>
>> I strongly object to the "src.empty()" syntax, as it is too
> restrictive --
>> it forces InputRange to be a class type that already has a
>> member function "empty" defined. For example, I couldn't use a
> pair of
>> iterators as my range. I would prefer an "empty(src)" syntax.
>>
>> I'm going to go out on a limb here and say that I am increasingly
> of the
>> opinion that non-static member functions are usually a bad idea, as
> they
>> produce brittle designs and expose implementation decisions.
>
> OO experts will strongly disagree about all of this. The designs are
> in fact the opposite of brittle since full encapsulation is
> employed. Further, there's no way you can claim that they expose
> implementation decisions since they do exactly the opposite.

I think you both more or less correct. I don't quite like the choice of
operations on the range in the above example - I think the idea of
incrementing a range to mean "advance the beginning" to be a bit awkward and
using this implementation in practice would be a bit cumbersome. Try this:

template <typename InputRange, typename OutputIterator>
OutputIterator copy(InputRange src, OutputIterator dst)
    {
    typedef range_t<InputRange> range;
    range input(make_range(src));
    for (range::iterator iter(input.begin()), !input.is_end(iter), ++iter)
        { *dst = *iter; }
    return dst;
    }

I haven't compiled this so it may not be exact... The range_t<> and
make_range()<> allow the copy operation to work on anything that can be
turned into a range. Any container or array type can be used to construct a
range. The form used by boost::array_traits could also be used (which should
be renamed to container traits) but could alos be expanded so that the end
test doesn't rely on the existence of an end iterator. This allows for
sentry based ranges.

Sean

-- 
Sean Parent
Sr. Computer Scientist II
Advanced Technology Group
Adobe Systems Incorporated
sparent_at_[hidden]

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