Backup of David's Livejournal

Sometimes you need lambda::bind()


Note to self, next time you mix smart pointers and lambda, remember what to do if things don't resolve automatically. Ex. #include <iostream>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

using namespace std; using namespace boost;
using namespace lambda;

struct B {   explicit B(int a) : a_(a) {}
  int get_a() const { return a_; }
private:
  int a_;
};

ostream& operator << (ostream &s, const B &b) {
  s << b.get_a();   return s;
}

int main() {
  shared_ptr<B> b1(new B(10));   shared_ptr<B> b2(new B(5));
  vector<shared_ptr<B> > v;
  v.push_back(b1);
  v.push_back(b2);
  for_each(v.begin(),v.end(), cout << *_1 << ' ');
  cout << endl;
  cout << count_if( v.begin(), v.end(), bind(&B::get_a, *_1) > 7);   return(0); }

Comments

 sjonsvenson on Dec 1st 2005 at 2:09 PM
Nope, I don't know that language.

 dblume on Dec 1st 2005 at 2:30 PM
I don't know if you're being facetious. It is arcane, but it is also clear, concise, and complete. Some people would read this and wonder what a complete n00b I am. Others will read it and find it impenetrable. It's amazing the breadth of skill levels in this discipline.

 sjonsvenson on Dec 2nd 2005 at 12:06 AM
No. I just mean I never learned C or any of it's derivatives/family. I learned to program in Pascal (TP3) and now I program in RPG (in the IBM AS400/eServer). I can read C and Java but only if it's very simple coding, like one step up from 'hello world'.