简体   繁体   中英

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 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM