Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-11-28 19:46:33


Rodolfo Lima wrote:
>> You are right, it doesn't work; lambda's operator->* seems a pretty
>> odd beast. Interesting that nobody has discovered its quirks so far.
>> FWIW, the syntax above is how it should work in order to be usable.
>> Lambda currently implements
>
> That's how I thought it would work. Is it difficult to make lambda
> work this way?

No idea. :-)

>> You can use the above syntax with boost::bind and overload
>> get_pointer for aux, but this won't work for lambda::bind or
>> std::tr1::bind. Can't you just
>> add operator* to aux? A raw pointer can escape via ptr, so adding a
>> way to dereference the pointer doesn't seem to make it any more
>> dangerous.
>
> No, I can't add operator* to aux because its ptr member function
> actually returns a shared_ptr that it gets from another source. If
> this source doesn't own the shared_ptr, ptr will return a shared_ptr
> with use_count 1. If we define this member as
>
> T &operator*() { return *ptr(); }
>
> it would return a reference to an object that is already deleted by
> ~shared_ptr. By using only ->* or -> returning a shared_ptr, it will
> last until the end of the member function call.

I see... then a get_pointer overload won't work either, since you can't
return a raw pointer from it, and shared_ptr doesn't define operator->*. If
you can't cache the shared_ptr locally in aux, the only remaining option is
two nested binds.

>> It works for your example and returns 5. When I add an aux_ptr class
>> with a
>
> My example doesn't compile with g++ 4.1.1, which compiler are you
> using?

I happened to use VC 8.0 for the below. But this is just a variation of the
example you posted; I didn't know about the shared_ptr trickery yet.

#include <boost/function.hpp>
#include <iostream>

struct aux
{
    int a() const { return 5; }
};

struct aux_ptr
{
    aux * px_;
};

aux * get_pointer( aux_ptr const & p )
{
    return p.px_;
}

int main()
{
    boost::function<int(const aux_ptr &)> f = &aux::a;

    aux x;
    aux_ptr px = { &x };

    std::cout << f( px ) << std::endl;
}


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