Boost logo

Boost Users :

From: Jaakko Jarvi (jajarvi_at_[hidden])
Date: 2004-01-29 12:10:03


On Thu, 29 Jan 2004, Shawn Church wrote:

> I may be missing something simple here but I have tried everything I can think
> of
> and cannot get the following to work (simplified from a project I am working
> on):

Lambda and format don't play well together, I noticed :(
Here's a code that works however, with some explanation.
Not pretty.

Best, Jaakko

#include <iostream>
#include <iomanip>

#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <vector>
#include <algorithm>

#include <boost/format.hpp>

class foo{

public:
foo(const char* name): _name(name) {}
const char* name() const {return _name.c_str();}

private:
std::string _name;

};

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

int main() {

  std::vector<foo> Foos;
  Foos.push_back(foo("one"));
  Foos.push_back(foo("two"));

  for_each(Foos.begin(),
           Foos.end(), cout << bind(&foo::name, _1) << "|");

  format f("%-10s|") ;
  for_each(Foos.begin(), Foos.end(),
    cout << ret<format&>(var(f) % bind(&foo::name, _1)));

}

// format defines operator% as a member, which takes precedence over
// % defined by lambda. Therefore one must make the format object to
// be a lambda functor. var does that.
// Var, however, cannot take a temporary object (it holds a reference to
// the wrapped object). That's why the variable f.
// ret<format&> informs lambda about the return type of formats % operator.


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