Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-01-07 16:51:58


Server Levent Yilmaz wrote:

> // A function object generator.
> // It returns a functor which in turn returns some
> // double member of A accessed via address offset
> boost::function<double(const A&)> getmem
> (
> const A* obj,
> const double* mem
> )
> {
> ptrdiff_t shift = mem -
> reinterpret_cast<const double*>(obj);
> using namespace boost::lambda;
> return *(ll_reinterpret_cast<const double*>(&_1)
> + constant(shift) );
> }

This definitely sounds like a compiler bug. But your code isn't quite
standard-conforming. You ought to use a pointer to member, not
reinterpret_cast tricks.

boost::function<double(const A&)> getmem( double A::* pm )
{
    return pm;
}

> int main()
> {
> A obj;
> obj.a = 1.;
> obj.b = 2.;
> //
> foo( getmem(&obj, &obj.b), obj );

    foo( getmem( &A::b ), obj );

> }


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