Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-11-28 18:49:31


Rodolfo Lima wrote:

>> function<int(const aux &)> f = (&_1->*&aux::a)();
>
> This doesn't work because you are evaluating the functor without
> arguments, whereas it requires one.

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

aux x;

(&_1->*&aux::a)( x )();

instead of

(&_1->*&aux::a)()( x );

The current functionality doesn't make much sense to me; if you had x at the
time the lambda expression is constructed, you could have used it directly
and skip the _1 business.

>> function<int(const aux &)> f = bind( &aux::a, _1 );
>
> This would work, but I in my project I have to call operator->*
> because in my case aux is a class that models a pointer, but doesn't
> have a dereference operator, just operator->* and operator->, those
> are the only ways to access the containing pointer. Well, there's
> also a ptr member function that returns it, but I'm trying to achieve
> the most natural way to access the containing pointer members. By
> using aux's ptr member, I have to use bind functions, which is a
> little unnatural.

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.

>> or
>>
>> function<int(const aux &)> f = &aux::a;
>
> That doesn't make it either because f(a) just would return a delayed
> call to &aux::a, not its actual result.

It works for your example and returns 5. When I add an aux_ptr class with a
get_pointer overload,

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

works through it, too.


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