Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2003-10-11 04:37:25


"Brian McNamara" <lorgon_at_[hidden]> wrote in message
news:20031011032816.A12603_at_maui-lnx.cc.gatech.edu...
> On Sat, Oct 11, 2003 at 04:54:21PM +1000, Thorsten Ottosen wrote:
> > I'm playing a little with some pointer containers and would like some
> > iterators s.t
> >
> > class Base;
> > bool operator<( const Base& r, const Base& l );
>
> This may be your flaw right here.
>
> Usually people want to be able to compare Dogs with Dogs and Cats with
> Cats, but not Dogs with Cats. However if you define a less-than
> operator like that, you have to be able to compare any two Animals.
>
> If this is really what you intend to do (perhaps it is), then I think
> you shouldn't have any problems implementing what you're trying to
> implement.
>
> But if instead, you only want to be able to compare Dogs to Dogs and
> Cats to Cats, then the whole idea of sorting a ptr_vector<Animal*> is
> flawed. You can't sort a container when the elements aren't necessarily
> comparable to one another.

Brian, thanks for your cat and dog example; the thing is that it could be
necessary to sort polymorphic types or that Base is not polymophic. Anyway,
the hard thing is to make the copy copy pointers and not objects. The easy
solution
(which I'm leaning a little towards ) is simply to provide normal iterators:

struct ptr_less_than
{
    template< typename T >
    bool operator()( const T*l, const T* r)
   {
      *l < *r;
}
};

sort( v.ptr_begin(), v.ptr_end(), ptr_less_than() );

cheers

Thorsten


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