1

I'm here learning how to use boost::lambda. One question I have is about member function calling. It's just a test, and I'd like to do this with boost::lambda, as there are, obviously, half a million ways to copy the elements from one container to another container.

I have a list<int> which has 3 elements:

std::list<int> a;
a.push_back(2);
a.push_back(3);
a.push_back(4);

And a vector<int>:

vector<int> b;

I'm trying to do the following: for each element in a, push it back in b. Here's my shot:

std::for_each(a.begin(), a.end(), (b ->* (&std::vector<int>::push_back))(_1) );

The problem is that it's not accepting the member function call, telling:

no match for ‘operator->*’ in ‘b ->* &std::vector<int, std::allocator<int> >::push_back’

I tried some other ways, but they didn't work either.

Thanks in advance.

manlio
  • 18,345
  • 14
  • 76
  • 126
Gabriel
  • 1,803
  • 1
  • 13
  • 18

2 Answers2

1

I would advise you to use Boost.Phoenix with is a better implementation of lambda facilities in C++ and already has lazy version of std containers methods.

http://www.boost.org/doc/libs/1_46_1/libs/spirit/phoenix/doc/html/index.html

Phoenix is currently hidden inside Spirit but it is planned to become a first class boost citizen in 1.47 which is due soon.

Joel Falcou
  • 6,247
  • 1
  • 17
  • 34
1

Did you try something like this?

for_each(a.begin(), a.end(), bind(&std::vector<int>::push_back, &b, _1));
John Zwinck
  • 239,568
  • 38
  • 324
  • 436