lambda 绑定问题?
我是一个有提升的新手。这是我的测试代码,
using namespace boost::lambda;
std::vector<std::string> strings;
strings.push_back("Boost");
strings.push_back("C++");
strings.push_back("Libraries");
std::vector<int> sizes;
std::for_each(
strings.begin(),
strings.end(),
bind(
&std::vector<int>::push_back,
sizes,
bind<std::size_t>(&std::string::size, _1)));
std::for_each(sizes.begin(), sizes.end(), var(std::cout)<<_1);
构建项目并产生错误:
error C2665: 'boost::lambda::function_adaptor::apply' : 两个重载都不能转换所有参数类型
我想知道出了什么问题?真的很感激。
I am a new beginner with boost. And here is my test code,
using namespace boost::lambda;
std::vector<std::string> strings;
strings.push_back("Boost");
strings.push_back("C++");
strings.push_back("Libraries");
std::vector<int> sizes;
std::for_each(
strings.begin(),
strings.end(),
bind(
&std::vector<int>::push_back,
sizes,
bind<std::size_t>(&std::string::size, _1)));
std::for_each(sizes.begin(), sizes.end(), var(std::cout)<<_1);
build the project and yield an error:
error C2665: 'boost::lambda::function_adaptor::apply' : none of the 2 overloads could convert all the argument types
I wonder what's wrong? Really appreciate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一个问题是 std::vector::push_back 是 C++0x 中的重载函数(存在带有左值引用参数的重载和带有右值引用参数的重载)。
第二个问题是标准库成员函数的类型未指定:允许实现者向成员函数添加带有默认参数的额外参数,并且允许他们添加成员函数的额外重载,因此您所做的任何转换都不会被便携的。消歧示例假设我们忽略第二个问题。
您需要将指向成员函数的指针转换为正确的类型,否则这将无法与 C++0x 库实现一起使用(指向成员函数的指针将不明确)。您需要:
使用 C++0x 函数库,可以进行以下操作(如果将
std::
替换为boost::
,它也应该可以与 Boost 一起使用;我只是无法测试这一点):请注意,这真是一团糟,使用 for 循环会更清晰:
或者,如果您有支持 lambda 表达式的编译器:
或者,为了更有趣:
The first problem is that
std::vector::push_back
is an overloaded function in C++0x (there is an overload with an lvalue reference parameter and an overload with an rvalue reference parameter).The second problem is that the types of Standard Library member functions are unspecified: implementers are permitted to add extra parameters with default arguments to member functions, and they are permitted to add additional overloads of member functions, so any casting that you do would not be portable. The disambiguation example assumes that we ignore this second problem.
You will need to cast the pointer-to-member-function to the correct type, otherwise this won't work with a C++0x library implementation (the pointer-to-member-function will be ambiguous). You'll need:
Using the C++0x functional library, the following works (it should work with Boost as well if you replace
std::
withboost::
; I just can't test that):Note that this is a real mess and it's much clearer just to use a for loop:
Or, if you have a compiler that supports lambda expressions:
Or, for more fun:
或者您可以创建自己的函数对象:
然后使用它(注意用
ostream_iterator
和copy
替换另一个lambda
):Or you can create your own function object:
Then use it (note the
ostream_iterator
andcopy
replacing anotherlambda
):