不要作温水里的那只青蛙
来源:互联网 发布:linux 网站 编辑:程序博客网 时间:2024/04/28 09:50
问题的起因很简单,我实验一个简单的程序不过。这个程序的本意是想检验一下 boost.bind 在 1.33 中新加入的比较运算符,我以为这一点也不难,然而程序的运行结果大出我的意料:
#include
#include
#include
#include
//#include
//#include
#include
//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 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::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 这一部分,现在我们将不得不显式的指出我们绑定到一个 data member 的目的是取得它的 reference 而不是一份拷贝。在修改这个问题之前,一定要小心处理这个问题,否则就会跟我一样目瞪口呆。
#include
#include
#include
#include
//#include
//#include
#include
//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
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
"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
区别在于 bind
- 不要作温水里的那只青蛙
- 不要作温水里的那只青蛙
- 不要作温水里的那只青蛙
- 不要作温水里的那只青蛙
- 不要作温水里的那只青蛙
- 不要作温水里的那只青蛙
- 温水里的青蛙
- 温水里的青蛙
- 不要让自己成为温水里的青蛙
- 不做温水里的青蛙
- 你是温水里的青蛙吗?
- 不要做温水中的青蛙
- 吃掉那只青蛙
- 《吃掉那只青蛙》
- 清闲的工作与温水煮青蛙;-)
- “温水煮青蛙”的实验论证
- 温水煮青蛙的安逸感
- 【学习分享】温水里的程序员,技术将成为温水。
- 书评--设计模式入门
- 避免在 SQL Server 中盲目地追求一句处理
- WinFX 将被重新命名为.Net Framework 3.0
- 转载:如何指定程序在Vista上面需要提升权限运行(Elevated)
- 软件质量目标度量
- 不要作温水里的那只青蛙
- (转)S60系统的文件操作类
- 【交流】SQL 2005溢用之:合并列值
- 在Win2K/XP/2K3中 模拟实现VISITA效果对话框
- Google发布IM软件--Talk
- 泛型算法:Tips (3) --- 初始化
- 如何知道可执行文件是32-bit还是64-bit
- [c++]一个对MSXML的封装类 使用C++的Smart Pointer 对MSXML DOM封装,可以方便地访问xml文件
- 【交流】SQL 2005溢用之:分拆列值