🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2050

Re: Making STL user-friendly?? [Re: STL find errorprone]

#2050
From: Mumit Khan
Date: Tue, 26 Sep 1995 21:01
119 lines
3910 bytes
Fabrice Clerc <clerc@gla.ecoledoc.ibp.fr> wrote in comp.std.c++:
> maybe make containers of pointers a bit easier to use:
> 
> o allow "standard" comparison functions to be used with
>   containers of pointers:
> 
>   set<int*,indirect_apply<less<int> > > spi;
> 
>   (boy, that sure is synctatic sugar :-)
> 

I always have and still do contend that if you have pointers in your
containers, you should revisit the design: if you're storing pointers
to basic/built-in types, why bother; if you're storing pointers to
classes either for efficiency/space or for polymorphism, use a wrapper.
See http://www.xraylith.wisc.edu/~khan/software/stl/STL.newbie.html for
some rationale and examples of this.

With that out of the way, how about the following: (off the top of my 
head, so not tested yet)


    #include <stl.h>

    template <class BinaryFunction>
    class binary_dereference : binary_function<
	BinaryFunction::first_argument_type,
	BinaryFunction::second_argument_type, 
	BinaryFunction::result_type> 
    {
    public:
	binary_dereference(const BinaryFunction& func) : func_(func) { }
	BinaryFunction::result_type operator() (
	    const BinaryFunction::first_argument_type*& x,
	    const BinaryFunction::second_argument_type*& y
	) const {
	    return func_(*x, *y);
	}
    protected:
	BinaryFunction func_;
    };

    //
    // useful adapter for binary_dereference function object
    //
    template <class BinaryFunction>
    inline binary_dereference<BinaryFunction>
    dereference2 (const BinaryFunction& func) 
    {
	return binary_dereference<BinaryFunction>(func);
    }

    //
    // define and populate deque<int*> ...
    //

    deque<int*> deque1;


    //
    // now you can do either of the following...
    //

    sort(
	deque1.begin(), deque1.end(), 
	binary_dereference<less<int> > (less<int>())
    );

    //
    // or better off using the adapter, dereference2
    //
    sort(
	deque1.begin(), deque1.end(), 
	dereference2 (less<int> ())
    );


or you could always do what ObjectSpace does -- define new global fcts
(eg., less_p which deferences the args before using them).


> 
> 
> o also, I have found
> 
>   template <class Iterator>
>   void delete_range(Iterator first,Iterator last) {
> 
>     while (first!=last) {
>       delete *first++;
>     }
>   }
> 
>   useful.
> 

Yup very much so. Again see my STL page for more of these.

The real question is whether these "convenience" functions/algos should
actually be (have been) part of the standard? My personal belief is that
these are way too specific to programming style and the particular 
application, and should be left to the user to add to the STL base,
preferably in a different namespace.

> ---
> Fabrice Clerc
> clerc@gla.ecoledoc.ibp.fr
>

regards,
mumit -- khan@xraylith.wisc.edu
http://www.xraylith.wisc.edu/~khan/


Cc: comp.std.c++, clerc@gla.ecoledoc.ibp.fr

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <449o3m$17jm@news.doit.wisc.edu>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
References: <DED5p2.I2I@online.tmx.com.au> <4378m2$1n86@news.doit.wisc.edu> <MATT.95Sep14103019@physics10.berkeley.edu> <MATT.95Sep22101543@physics11.berkeley.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: khan@xraylith.wisc.edu (Mumit Khan)
Newsgroups: comp.std.c++
Subject: Re: Making STL user-friendly?? [Re: STL find errorprone]
Date: 26 Sep 1995 21:01:40 GMT
Organization: Center for X-ray Lithography, UW-Madison
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <449o3m$17jm@news.doit.wisc.edu>
References: <DED5p2.I2I@online.tmx.com.au> <4378m2$1n86@news.doit.wisc.edu> <MATT.95Sep14103019@physics10.berkeley.edu> <MATT.95Sep22101543@physics11.berkeley.edu>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: modi.xraylith.wisc.edu
Content-Length: 2879
X-Lines: 115
Originator: clamage@taumet