简体   繁体   English

自动推断bind1st(mem_fun(&my_class :: f),this)的类型?

[英]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 . 我想将bind1st(mem_fun(&my_class::f), this)函数传递给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.) (我知道mem_fun正是因为这个原因为我们节省了很多痛苦。)

This depends on the argument and return types of my_class:f. 这取决于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: 这种情况在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. 而且,如果您使用boost,则不需要使用BOOST_AUTO ,因为您可以只使用boost::bind并使其可读,而无需本地。

for_each(v.begin(), v.end(), boost::bind(&my_class::f, this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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