C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange behavior of boost::bind

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Ralph Zhang
Guest





PostPosted: Sun Aug 14, 2005 6:50 pm    Post subject: strange behavior of boost::bind Reply with quote



In the following program, I try to find a Person object in a map whose
Name() == "Ralph". I use boost::bind and boost::lambda, they both
compiles on my VC7.1, but the behavior is different. Boost::lambda
outputs 234, which is I expected, but boost::bind outputs 0, I don't
know where does this result come from. What's wrong with boost::bind?


#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 "Ralph" )->first;

}

using boost::lambda, the result is:

Ralph's Id is: 234

but with boost::bind, the result is:

Ralph's Id is: 0

compiler is VC7.1. What happened?


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Peter Dimov
Guest





PostPosted: Mon Aug 15, 2005 10:53 am    Post subject: Re: strange behavior of boost::bind Reply with quote



Ralph Zhang wrote:

Quote:
class Person
{

[...]

Quote:
std::string& Name()
{ return name_; }
[...]


Quote:
};

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 "Ralph" )->first;

}

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.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.