RE: [Boost-users] [BLL] How to search in a list for an object comparing a data member?
Gianluca Silvestri
Hi all, What I want to achieve is a code similar to this:
struct Foo { int fDataMember;}
int main() { std::vector<Foo> foos; Foo fo; std::find_if(foos.begin(), foos.end(), _1->fDataMember == fo.fDataMember); //<-- won't compile; }
Well, apart from the missing semi-colon, missing includes and missing closing brace, I imagine you're having trouble with "_1->fDataMember". This is a documented limitation of the lambda library - there is no way to defer lookup of member names (overloading -> just changes the type and value of the pointer used) so member access can't be done on a placeholder.
I know I could use a functor object but this is just an example for a more general need.
Sorry, you'll have to define functors. These template functor classes
might be useful:
template
struct Foo { int fDataMember;}
int main() { std::vector<Foo> foos; Foo fo; std::find_if(foos.begin(), foos.end(), _1->fDataMember == fo.fDataMember); //<-- won't compile; }
As your vector is of Foo, not Foo*, did you mean "_1.fDataMember" ?
If so, you can do this:
std::find_if(foos.begin(), foos.end(),
bind(&Foo::fDataMember,_1) == fo.fDataMember);
That assumes:
#include
I imagine you're having trouble with "_1->fDataMember".
I didn't realize there was a limitation when using "->". Surely there must already be something in the lambda library to deal with this? It seems like something that must come up a lot. Darren
-----Messaggio originale----- Da: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] Per conto di Darren Cook Inviato: venerdì 31 ottobre 2003 14.31 A: Boost Users mailing list Oggetto: Re: [Boost-users] [BLL] How to search in a list for an objectcomparinga data member?
struct Foo { int fDataMember;}
int main() { std::vector<Foo> foos; Foo fo; std::find_if(foos.begin(), foos.end(), _1->fDataMember == fo.fDataMember); //<-- won't compile; }
As your vector is of Foo, not Foo*, did you mean "_1.fDataMember" ?
If so, you can do this: std::find_if(foos.begin(), foos.end(), bind(&Foo::fDataMember,_1) == fo.fDataMember);
That assumes: #include
#include using namespace boost::lambda; I imagine you're having trouble with "_1->fDataMember".
I didn't realize there was a limitation when using "->". Surely there must already be something in the lambda library to deal with this? It seems like something that must come up a lot.
Darren
Thanks Gianluca
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/bo> ost-users
Darren Cook
struct Foo { int fDataMember;}
int main() { std::vector<Foo> foos; Foo fo; std::find_if(foos.begin(), foos.end(), _1->fDataMember == fo.fDataMember); //<-- won't compile; }
As your vector is of Foo, not Foo*, did you mean "_1.fDataMember" ?
If so, you can do this: std::find_if(foos.begin(), foos.end(), bind(&Foo::fDataMember,_1) == fo.fDataMember);
That assumes: #include
#include using namespace boost::lambda; I imagine you're having trouble with "_1->fDataMember".
I didn't realize there was a limitation when using "->". Surely there must already be something in the lambda library to deal with this? It seems like something that must come up a lot.
Have you tried something along the lines of: (&_1)->*&Foo::fDataMember == var(fo.fDataMember) or bind(&Foo::fDataMember,_1) == var(fo.fDataMember) ?? -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (4)
-
Ben Hutchings
-
Darren Cook
-
David Abrahams
-
Gianluca Silvestri