Boost logo

Boost :

From: Shane Beasley (sbeasley_at_[hidden])
Date: 2002-09-17 07:07:15


On Tue, 17 Sep 2002, David B. Held wrote:

> I would recommend boost::bind(). All compose_f_gx() does is chain
> two bind calls together (effectively). You can call nested bind()s, of
> course.
> You want something like:
>
> boost::bind(select2nd(), boost::bind(func, _1))
>
> As you can imagine, select2nd just helps you get at the second member
> of a pair. A lot of people write this themselves, but there may be
> something useful in boost::tuples.

That's func(x).second, not func(x.second), isn't it? I'm trying to do
something like the latter.

Actually, right after I wrote my previous message, I figured out how to do
something similar to compose_f_gx with boost::bind. From the docs:

  bind(f, bind(g, _1))(x); // f(g(x))

I actually got this to work for a const member function:

  std::for_each(m.begin(), m.end(),
    boost::bind(&Foo::f,
      boost::bind(&std::map<int, Foo>::value_type::second, _1)));

However, if Foo::f is a non-const member function, it still fails to
compile. The error:

/usr/include/boost/bind.hpp:163: initialization of non-const reference
type `class Foo &'
/usr/include/boost/bind.hpp:163: from rvalue of type `Foo'
/usr/include/boost/bind/mem_fn_template.hpp:55: in passing argument 1 of
`boost::_mfi::mf0<void,Foo>::operator ()(Foo &) const'

Hmm.

That it says "rvalue" troubles me -- it's making copies of the Foo objects
in the map before applying the function. Obviously, the point of applying
a non-const function to a sequence is to alter objects in the sequence;
working with copies won't do much good, which is precisely why copies
can't be passed by non-const reference and thus why this code does not
compile. In a way, I'm glad that this doesn't compile; I'd hate to see
this code do nothing useful, despite outward appearances to the contrary.

Thanks for the suggestion, though!

- Shane


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk