[英]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.