[英]Automatically deducing the type of bind1st(mem_fun(&my_class::f), this)?
我想将bind1st(mem_fun(&my_class::f), this)
函数传递给for_each
。 不幸的是,这很难读,所以我想给它起一个更易读的名称,如下所示:
(the type I am looking for) meaningful_name = bind1st(mem_fun(&my_class::f), this);
for_each(v.begin(), v.end(), meaningful_name);
有没有一种简单的方法可以推断函子的类型? (我知道mem_fun
正是因为这个原因为我们节省了很多痛苦。)
这取决于my_class:f的参数和返回类型。 如果功能是
T my_class::f(A arg)
那你需要
binder1st<mem_fun1_t<T,my_class,A> > meaningful_name = bind1st(mem_fun(&my_class::f), this);
这种情况在C ++ 0x中会更好:
auto meaningful_name = bind1st(mem_fun(&my_class::f), this);
不,没有简单的方法。 类型名称将相当长,甚至更不可读。 而且,如果您使用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.