描述你的问题
这是c++ primer 的一道习题,练习10.24,其中有一行代码
auto predicate = [&](int i){ return bind(check_size, str, i)(); };
这行lambda表达式中为什么要在bind()后面多增添一个括号?若不添括号则报错
贴上相关代码(不添括号的情况下)
auto check_size(string const& str, size_t sz){
return str.size() < sz;
}
auto find_first_greater(vector<int> const& v, string const& str){
auto predicate = [&](int i){ return bind(check_size, str, i); };
return find_if(v.cbegin(), v.cend(), predicate);
}
贴上报错信息
严重性 代码 说明 项目 文件 行
错误 C2451 “std::_Binder<std::_Unforced,bool (__cdecl &)(const std::string &,size_t),const std::string &,int &>”类型的条件表达式是非法的 ConsoleApplication5 c:\program files (x86)\microsoft visual studio 14.0\vc\include\algorithm 43
贴上相关截图
Because the
return
statement within the curly braces of lambda needs to return abool
,bind(...)
returns a callable, andbind(...)()
calls the returned callable, thus returning abool
.Personally, I think it is enough to use
std::bind
or lambda alone, but the questioner used lambda to packagestd::bind
once. The purpose of usingstd::bind
or lambda is to wrap a function that requires multiple parameters into a function with only one parameter, so that it can be used in the parameters of various standard library algorithm functions that require unary predicate.The code can be found here. The usage of
std::bind
is shown in the following fragment:Specifically, the third parameter of
std::find_if
requires an unary predicate.std::bind
is a function adapter, which returns a callable object, and this callable object requires only one parameter (only one placeholder, the rest of the parameters are provided). So it can be used in the third parameter position ofstd::find_if
.I am also a beginner, please correct me if I am wrong.
This thing is a callable object. If you want to return a bool, but you return this, an error will definitely be reported when used in find_if.
This is equivalent to calling this callable object, and what you get is a bool value.
Also, I think auto is a bit abused?
The above and below are the same effect, and auto means that you don’t need to write the return type. This writing may require a version after c++11 to support it