Re: Member function as parameter of STL algorithm
On Jul 19, 4:26 pm, dasjotre <dasjo...@googlemail.com> wrote:
On 18 Jul, 16:30, many_years_after <shua...@gmail.com> wrote:
for_each(
vec.begin(),
vec.end(),
mem_fun_ref(&Point::print)); // OK
sort(vec.begin(), vec.end(), mem_fun_ref(&Point::LargeThan)); //
ERROR
return 0;
}
what is the reason?
std::sort takes binary predicate as third argument
mem_fun_ref(&Point::LargeThan) returns unary
function that takes reference to a Point object.
No. In this case, mem_fun_ref returns a binary functional
object that takes a reference to a reference as its second
argument. It doesn't compile because the language doesn't allow
references to references.
The function adaptors in the original standard are seriously
broken, and close to useless in practice. Boost has solutions
to this; they were adopted in TR1, and will be part of the next
version of the standard. So the "correct" answer here is to
forget about mem_fun_ref, and use the Boost stuff.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34