Topic: Problem with mem_fun_t and mem_fun1_t
Author: Saroj Mahapatra <saroj@nynexst.com>
Date: 1997/02/15 Raw View
Often we want to call a member function on a collection of polymorphic
objects and mem_fun was designed for such situtations. When the member
function does not return anything(void), mem_fun_t<void, T> can not
be used, as its operator() tries to return (p->*pm)() and gives a
compilation error. Probably a specialization of mem_fun_t for R == void
can solve this problem (to return nothing in operator() ).
example: class View { public: void display(); }
void foo() { for_each(views_.begin(), views_.end(),
mem_fun(View::display)); }
will not compile without specialization.
The specialization solution will fail in case of mem_fun1_t and bind2nd
combination. For example,
class View { public: void addVertex(Vertex* v); }
void foo(Vertex* v) {
for_each(views_.begin(), views_.end(),
bind2nd(mem_fun1(View::addVertex), v));
}
will not compile even if mem_fun1_t has specialization for R == void
to return nothing. In this case, probably the specialization for void
should return a dummy int( may be 0) in operator().
I think these two specializations should be made part of the standard;
otherwise everybody will end up writing explicit loop or such
specializations (probably in incompatible ways).
What are your opinions?
Thanks,
Saroj Mahapatra
Unix/C++/Motif consultant
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]