Implementing normal algorithms using predicate versions

chris caj@cs.york.ac.uk
Sun Nov 28 15:59:00 GMT 2004


Paolo Carlini wrote:

> 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...
>>>   
>>
>> 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"...
>
> Chris, Nathan, do you have already something more specific in mind or 
> maybe
> my point above is wrong?!?
>
I was intending on using something like (warning: away from C++ 
compiler, I haven't even compiled this code!)

template<typename __T1, typename __T2>
struct __default_predicate {
bool
operator()(const __T1& __t1, const __T2& __t2) { return __t1<__t2;}
};

Or perhaps even just (what I tested with)
template<typename __T1, typename __T2>
bool __default_predicate(const __T1& __t1, const __T2& __t2) { return 
__t1<__t2;}

Chris



More information about the Libstdc++ mailing list