Automatically deducing the type of bind1st(mem_fun(&my_class::f), this)?
I would like to pass the bind1st(mem_fun(&my_class::f), this)
functor to for_each
. Unfortunately it is very difficult to read so I would like to give it a more readible name like this:
(the type I am looking for) meaningful_name = bind1st(mem_fun(&my_class::f), this);
for_each(v.begin(), v.end(), meaningful_name);
Is there a simple way to deduce the type of the functor? (I know mem_fun
saves us a lot 开发者_StackOverflow中文版of pain exactly for this reason.)
This depends on the argument and return types of my_class:f. If the function is
T my_class::f(A arg)
then you need
binder1st<mem_fun1_t<T,my_class,A> > meaningful_name = bind1st(mem_fun(&my_class::f), this);
This kind of thing will be nicer with C++0x:
auto meaningful_name = bind1st(mem_fun(&my_class::f), this);
No there is no simple way. The type name will be rather long and even more unreadable. And if you use boost, you don't need to use BOOST_AUTO
, because you can just use boost::bind
and have it readable, without a need for a local.
for_each(v.begin(), v.end(), boost::bind(&my_class::f, this));
精彩评论