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
#include
#include <vector>
#include <algorithm>
#include
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(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 informs lambda about the return type of formats % operator.