要理解這一點,請思考如果您只是將文字傳遞給bind
的3 rd參數,這將意味着什麼。例如,如果你做了:
accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, 13))
結果將是size(foos) * 13
,因爲plus
會用13
,因爲它是在每次迭代加數。
accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, mem_fn(&foo::r)))
不能編譯,因爲它試圖爲被加數plus
傳遞的mem_fn(&foo::r)
結果。既然不能轉換成int
plus
不能接受。但即使它可以轉換爲int
,但這不是您要查找的內容,您需要參數並致電foo::r
,並將結果傳遞給plus
。因此我們知道我們需要看到,在該陳述中的某處使用placeholders::_2
,傳達了其調用r
方法的參數。
我們需要綁定placeholders::_2
綁定到一個仿函數,它將調用該方法r
上它的參數。綁定當然需要bind
,但實際上bind
can take a method as it's 1st argument。
也就是說,您的工作代碼中的bind(&foo::r, placeholders::_2)
聲明在非嵌套窗體中沒有任何意義;該仿函數甚至不需要2個參數!c++實際上具有special rules for handling a bind
nested within another bind
,使得它們可以共享外bind
的佔位符,恐怕沒有的方式來傳達綁定參數嵌套表達式:
If the stored argument arg is of type T
for which std::is_bind_expression<T>::value == true
(for example, another bind
expression was passed directly into the initial call to bind
), then bind
performs function composition: instead of passing the function object that the bind subexpression would return, the subexpression is invoked eagerly, and its return value is passed to the outer invokable object. If the bind
subexpression has any placeholder arguments, they are shared with the outer bind
.
在此使用mem_fn
的唯一方式表達方式是通過它的結果爲bind
爲傳送placeholders::_2
:bind(mem_fn(&foo::r), placeholders::_2)
這是有效的,但當簡單的bind(&foo::r, placeholders::_2)
就足夠了,這是一個不必要的步驟。因此產生函子最好的辦法是要麼你遞上聲明:
accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, bind(&foo::r, placeholders::_2)))
,或者通過使用拉姆達:
accumulate(cbegin(foos), cend(foos), 0, [](const int augend, const auto& addend) { return augend + addend.r(); })
你是第一人,我看到使用'bind'的特殊處理嵌套綁定表達式而不知道它。 –
mem_fn已棄用,請勿使用它。 –
@ n.m。它是?我知道它內部的typedefs將在C++ 17中,但我沒有看到有關'mem_fn'本身的任何內容被棄用。 – NathanOliver