Boost logo

Boost Users :

From: Phillip Hellewell (sshock_at_[hidden])
Date: 2006-08-23 12:50:19


On 8/23/06, Minkoo Seo <minkoo.seo_at_[hidden]> wrote:
>
> for_each(m.begin(), m.end(),
> bind(&map<string, int>::insert, &m2,
> bind(&make_pair,
> bind(&map<string, int>::value_type::first, _1),
> bind(&map<string, int>::value_type::second, _1))));
>
> Unfortunately, the above code fails to compile and I have no idea
> why it fails. I'm using g++ and the errors are as follows:
>

First off, just thought I would mention that for an example such as this,
regular boost::bind would probably suffice. You don't need lambda.

Second of all, I don't know which insert function you are trying to call,
but you should be able to write this code instead, which is much simpler:

   for_each(m.begin(), m.end(),
           bind(&map<string, int>::insert, m2, _1));

Finally, even that fails to compile! I believe the problem is that since
insert is an overloaded function, boost cannot figure out which one to
call. So you help it out by type-casting explicitly, like this:

   typedef pair<map<string, int>::iterator, bool> (map<string, int>::*
InsertFunc)(const map<string, int>::value_type&);

   for_each(m.begin(), m.end(),
           bind(static_cast<InsertFunc>(&map<string, int>::insert), m2,
_1));

It's ugly, but it works (actually, I tried with lambda and it still didn't
work, but with regular boost::bind it does).

Now after going to ALL that work just so you can use for_each, don't you
just want to forget it and use iterators?

   for( map<string, int>::const_iterator it = m.begin(); it != m.end(); ++it
)
       m2.insert(*it);

Phillip Hellewell



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