我是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
[如何才能做到这一点?
解决方法
#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; }
也更喜欢在std :: bind1st和std :: bind2nd since both were deprecated in C++11上使用std :: bind,并计划在C 17中删除.