Implementing normal algorithms using predicate versions

Gabriel Dos Reis gdr@integrable-solutions.net
Sun Nov 28 23:55:00 GMT 2004


Paolo Carlini <pcarlini@suse.de> writes:

| Nathan Myers wrote:
| 
| >On Sun, Nov 28, 2004 at 03:00:31PM +0000, caj wrote:
| >
| >> Many algorithms have a predicated version, and a version which
| >> simply uses operator<. Why not implement the operator< versions
| >> using the predicated versions? This would seem to half the size of
| >> the algorithm heaader, remove lots of redundant code, and on -O2
| >> seems to produce identical code...

Caj is right.  And I remember Benjamin and I discussed this stuff back
in 2002.

| >>
| >This is probably just historical.  When the STL was written, compoilers
| >just weren't very good.  Anywhere you can demonstrate that it actually
| >does produce the same code, nowadays, I think a patch would be welcome.
| >
| Interesting... In fact this issue is more general: should the library
| be "optimized"
| for -O2 or for -O0? Are we ready to trade some loss of performance at low
| optimization levels for the sake of header simplicity (or other
| ends)?!? To be clear,
| in the specific case at issue I would say yes (in principle)  ;)
| 
| Anyway, more to the point, I don't see clearly how you would *actually*
| implement the proposal. In my reading of the standard (17.4.3.1/1) an user
| of the library can definitely provide his own specialization of
| less<>, therefore,
| the trivial idea (i.e., calling the predicated version with less)
| seems a "no-no"...

This is done by using 

   namespace __gnu {
       struct less {
          template<class T>
            bool operator()(const T& lhs, const T& rhs) const
            { return lhs < rhs; }
       };
   }

That helps remove a significant amount of duplication in V3 headers.  I
did test it -- and discuss it with Benjamin; but never submitted the
full patch :-(

-- Gaby



More information about the Libstdc++ mailing list