簡體   English   中英

自動推斷bind1st(mem_fun(&my_class :: f),this)的類型?

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM