Re: Using std::less_equl predicate with std::min()
Vladimir Grigoriev wrote:
But I have formulated this another way. If a binary
predicate has value of true then 'b' is returned. Otherwise 'a' is
returned. So I do not see a problem. Moreover including std::less_equal
into std::min makes code readable and clear.
std::min( a, b, std::less_equal<T>() );
It is funny to note that the compiler allows to use std::greater instead
of std::less in the std::min in spite of that this looks strange.
Yes, strange indeed. However, it only gives you the first element according
to the given order. Using less in ascending order, otherwise descending
order.
Moreover I can show another example when applying std::less_equal is
useful. For example you want to find the last minimum element in a
sequence. Then you could use std::less_equal instead of std::less.
int a[] = { 6, 1, 7, 1, 8 };
int *p = std::min_element( a, a + sizeof( a ) / sizeof( *a ),
std::less_equal() );
Use a reverse iterator to search from the end. All search functions return
the first match, not the last.
In fact without any serious reason the Microsoft compiler prevents from
compiling of a valid code. In my opinion it is one mode serious bug of the
Microsoft compiler. I have not seen any reasonable explanation why this
code may not be compiled.
Look again, someone mentioned the C++ standard. ;)
It seems that Microsoft inserted this check
everywhere in its library without any thought about consequences
Nope, the strict ordering comes from the C++ standard which in turn took it
from the STL. You're barking up the wrong tree. BTW: I'd ask this on
comp.lang.c++.moderated, you have typically more C++ experts there.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932