我是C ++ 11 lambdas的新手,想通过绑定第二个参数将二元lambda变成一元lambda:
auto lambda1 = [] (int a, int b) { return a+b; };
auto lambda2 = std::bind2nd(lambda1, 5);
编译失败,
error: no type named 'first_argument_type' in 'struct main(int, char**)::<lambda(int, int)>'
class binder2nd
[如何]这样做?
答案 0 :(得分:12)
如果您已经使用了lambda表达式,只需使用另一个:
auto lambda2 = [&] (int i) {return lambda1(i, 5);};
答案 1 :(得分:8)
#include <functional>
int main(int argc, char *argv[])
{
auto lambda1 = [](int a, int b) { return a+b; };
auto lambda2 = std::bind(lambda1, std::placeholders::_1, 5);
// ~~~~~~~^^^^~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^~~~
return 0;
}
<强> Demo 强>
还希望std::bind
使用std::bind1st
和std::bind2nd
since both were deprecated in C++11,并计划在C ++ 17中删除。