问题的起因很简单,我实验一个简单的程序不过。这个程序的本意是想检验一下 boost.bind 在 1.33 中新加入的比较运算符,我以为这一点也不难,然而程序的运行结果大出我的意料:
#include <iostream>#include <algorithm>#include <map>#include <string>//#include <boost/lambda/lambda.hpp>//#include <boost/lambda/bind.hpp>#include <boost/bind.hpp>//using namespace boost::lambda;using namespace boost;class Person{public: Person(){} Person(const std::string& name) : name_(name){} std::string& Name() { return name_; }private: std::string name_;};int main(){ std::map<int, Person> persons; persons[123] = Person("Amy"); persons[234] = Person("Ralph"); persons[345] = Person("Simon"); persons[456] = Person("Maggie"); std::cout << "Ralph's Id is: " << std::find_if( persons.begin(), persons.end(), bind(&Person::Name, bind(&std::map<int, Person>::value_type::second, _1)) =="Ralph" )->first;}它的输出竟然是
Ralph's Id is: 0不过,如果把程序中的 bind 库换成 boost.lambda (uncomment 掉前面的 include 和 using 即可),输出就是
Ralph's Id is: 234这才是我想要的,我在新闻组提了这个问题,Peter Dimov 给我的答复可能是对我们每个使用 boost.bind 的人的忠告:
I get an error C2439 on this example; the problem is that the inner
bind returns a temporary Person by value, but Person::Name returns a
reference to its member std::string.
bind on a data member was changed to return a value and not a reference
because of similar reference to temporary issues, but it seems clear
now that this change has caused more problems than it solved.
I'll try to fix it for the next release (1.33.1 if there is one, or
1.34), but in the meantime you need to use
bind<Person&>(&std::map<int, Person>::value_type::second, _1)
to get the old reference-returning behavior back.
区别在于 bind
<Person&> 这一部分,现在我们将不得不显式的指出我们绑定到一个 data member 的目的是取得它的 reference 而不是一份拷贝。在修改这个问题之前,一定要小心处理这个问题,否则就会跟我一样目瞪口呆。
分享到:
相关推荐
boost stl一些常用函数的使用训练
算是一个消息处理框架吧,用于说函数对象function与bind的基本用法; 比较适合入门boost的function与bind的基本用法
c++11使用bind11的例子,对学习c++11 的新特性有所帮助。
bind bind bind bind
Björn Karlsson为中级至高级的C++开发者描述了所有58个Boost库的轮廓,并完整叙述了12个可能最有用的库。Karlsson的主题范围从智能指针和类型转换,到容器和数据库结构,解释了如何正确地使用每一个库来改进你的...
前端开源库-react-autobindreact autobind,自动将组件类中定义的方法绑定到当前对象的词法
基于boost的asio封装的高性能TCP服务器。asio已经有很好的事件封装机制,只有底层...3、运用最新的C++11语法规范实现全部代码(lamda override bind) 原创博客地址: http://blog.csdn.net/wang19840301 欢迎留言讨论
BIND9中文手册 BIND9中文手册 BIND9中文手册 BIND9中文手册 BIND9中文手册
3.2 Boost.Bind 3.3 Boost.Ref 3.4 Boost.Function 3.5 Boost.Lambda 3.6 Exercises Chapter 4: Event Handling 4.1 General 4.2 Signals 4.3 Connections 4.4 Exercises Chapter 5: String Handling 5.1 General ...
赠送jar包:jakarta.xml.bind-api-2.3.3.jar; 赠送原API文档:jakarta.xml.bind-api-2.3.3-javadoc.jar; 赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-...
BIND9配置手册 BIND9.9管理员手册 DNS配置手册
赠送jar包:jakarta.xml.bind-api-2.3.3.jar; 赠送原API文档:jakarta.xml.bind-api-2.3.3-javadoc.jar; 赠送源代码:jakarta.xml.bind-api-2.3.3-sources.jar; 赠送Maven依赖信息文件:jakarta.xml.bind-api-...
BIND9 IPV6操作指南 BIND9 IPV6操作指南
ES5的bind手写实现
Bind9配置参考手册
dns,bind,排错,通过看日志找问题。
BIND9 参数详解 SC的bind一直以来基本上都是DNS的工业标准,不过BIND一直是漏洞不断,直到出了BIND9,isc的开发人员对bind9进行了重写,才相对好了一点
c++11引入了std::bind及std::function,实现了函数的存储和绑定,即先将可调用的对象保存起来,在需要的时候再调用。定义了SignalObject信号类和SlotObject槽类,其中信号类中的 std::function(int)> _call就是要...
bind-9.3.6-4.P1.el5.i386.rpm bind-chroot-9.3.6-4.P1.el5.i386.rpm bind-devel-9.3.6-4.P1.SEL5_4.2.i386.rpm bind-libs-9.3.6-4.P1.el5.i386.rpm bind-utils-9.3.6-4.P1.el5_5.3.i386.rpm caching-nameserver-7.3...