On 8/23/06, Minkoo Seo <minkoo.seo@gmail.com> 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