ptr_fun with a lambda function
ptr_fun with a lambda function
我有下面的程序,它使用ptr_fun和lambda函数。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string target="aa";
vector<string> v1;
v1.push_back("aa");
v1.push_back("bb");
auto stringcasecmp=[](string lhs, string rhs)->int
{
return strcasecmp(lhs.c_str(), rhs.c_str());
};
auto pos = find_if(
v1.begin(), v1.end(),
not1( bind2nd(ptr_fun(stringcasecmp), target) )
);
if ( pos != v1.end())
cout << "The search for `" << target << "' was successful.n"
"The next string is: `" << pos[1] << "'.n";
}
我得到以下错误信息:
stackoverflow.cpp: In function ‘int main()’:
stackoverflow.cpp:21:41: error: no matching function for call to ‘ptr_fun(main()::<lambda(std::string, std::string)>&)’
stackoverflow.cpp:22:6: error: unable to deduce ‘auto’ from ‘<expression error>’
我如何修改代码(最低限度),使其编译?
bind2nd
(§D.9)和ptr_fun
(§D.8.2.1)在c++ 11中已弃用。您可以在find_if
中编写另一个lambda函数:
auto pos = find_if(v1.begin(), v1.end(),
[&](const std::string& s) {
return !stringcasecmp(s, target);
});
ptr_fun(<lambda>)
将不起作用,因为ptr_fun
是为c++ 03设计的,用于将函数指针转换为其他适配器的函数对象。lambda已经是一个函数对象,所以ptr_fun
是不必要的。
bind2nd
期望函数对象定义成员second_argument_type
和result_type
,这对于lambda来说不是真的,所以写bind2nd(<lambda>, target)
也不能工作。但是在c++ 11中有一个通用的替换:
std::bind(stringcasecmp, std::placeholders::_1, target)
但是,bind
没有返回not1
期望的c++ 03风格的函数对象:它需要bind
的result类型来定义不存在的argument_type
成员。因此最后的表达式
std::not1(std::bind(stringcasecmp, std::placeholders::_1, target))
将不工作。最简单的解决方法是使用我上面写的另一个lambda。
或者,您可以定义一个泛型否定子:
template <typename Predicate>
struct generic_negate
{
explicit generic_negate(Predicate pred) : _pred(pred) {}
template <typename... Args>
bool operator()(Args&&... args)
{
return !_pred(std::forward<Args>(args)...);
}
private:
Predicate _pred;
};
template <class Predicate>
generic_negate<Predicate> not_(Predicate pred)
{
return generic_negate<Predicate>(pred);
}
....
auto pos = find_if(v1.begin(), v1.end(), not_(bind(stringcasecmp, _1, target)));
示例:http://ideone.com/6dktf
尝试用pointer_to_binary_function<string,string,int>(stringcasecmp)
代替ptr_fun(stringcasecmp)
?
相关文章:
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将 lambda 表达式传递给 std::function in C++
- 从 std::function in C++ 访问模板化 lambda
- 如果 lambda 没有指定的类型,std::function 如何接受 lambda?
- 针对 std::function 的 lambda 表达式和模板推导:为什么会这样?
- std::function<std::optional<T>()> 如何与返回 T 的 lambda 兼容?
- 可以将模板化的 lambda 存储到 std::function 中吗?
- Visual Studio 2017中的哪些扩展在传入lambda时消除了"bool"与"std::function"的歧义?
- 使用 std::function 调用函数作为带有 lambda 的参数
- 使用 lambda/std::function 删除拥有该函数的对象
- std::function std::bind with lambda overload ambiguity
- c++ goto vs std::function lambda?
- 将 lambda 传递给 STL 集时出错"bad function call"
- Lambda 捕获'this'另存为 std::function
- lambda 和 std::function 的包装器
- C++ Lambda:访问 lambda 中的静态方法会导致错误'this was not captured for this lambda function'
- ptr_fun with a lambda function
- Setter of a lambda function?
- 如何传递lambda, function object或std::function作为模板参数并控制它们的参数