Boost logo

Boost Users :

From: Ben Hutchings (ben.hutchings_at_[hidden])
Date: 2003-10-31 05:39:26


Gianluca Silvestri <gianluca.silvestri_at_[hidden]> wrote:
> 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<typename C, typename V, const V C::* P>
    struct lookup_member_const
    {
        const V & operator()(const C & c) const
        {
            return c.*P;
        }
    };

    template<typename C, typename V, V C::* P>
    struct lookup_member
    {
        V & operator()(C & c) const
        {
            return c.*P;
        }
    };

Mind you, there might be similar things in Boost already.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net