Using boost::format with Lambda expressions
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): Given a class such as: class foo{ public: foo(const char* name): _name(name) {} const char* name() const {return _name.c_str();} private: std::string _name;}; I can do the following without any problems: std::vector<foo> Foos; ... for_each(Foos.begin(), Foos.end(),cout << bind(&foo::name, _1) << "|"); This prints a list of all the names in the vector of Foo objects by calling the Foo::name() method. It would be nice to be able to do something like the following: for_each(Foos.begin(), Foos.end(), cout << format("%-10s|") % bind(&foo::name, _1); I have tried everything I can think of including extending the return type deduction of the lambda library to include the "%" operator as used by format and binding directly to the basic_format::operator% method. In every case I get 40 or so compile errors explaining why my 30 character statement is wrong :-) Does anyone have an example of this usage that works? This is not a high priority as I can obviously just format the value returned by the Foo::name() method but it would be nice to have this technique available when the need arises. I adapated the code above from a test program that demonstrates the problem in detail. I can provide the test program if anyone is interested (just tell me what to do with it). Thankyou for any help you can provide, Shawn Church sl_church@sbc_global.net
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
On Thu, 2004-01-29 at 18:10, Jaakko Jarvi wrote:
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.
ah I hadn't thought about the interaction of the operator% with lambda. What would the situation be if the operator% were out of the class ? If it improves the usability with lambda, that would seem a reason enough, for me. In fact I had made it a member at first, because it needs access to the private data, but I had to put the real code in out-of-class functions anyhow because of msvc6 problems with member function templates, so I might as well make the whole operator% declared out of class. -- Samuel
On Jan 29, 2004, at 3:47 PM, Samuel Krempp wrote:
On Thu, 2004-01-29 at 18:10, Jaakko Jarvi wrote:
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. ah I hadn't thought about the interaction of the operator% with lambda.
What would the situation be if the operator% were out of the class ? If it improves the usability with lambda, that would seem a reason enough, for me. In fact I had made it a member at first, because it needs access to the private data, but I had to put the real code in out-of-class functions anyhow because of msvc6 problems with member function templates, so I might as well make the whole operator% declared out of class.
You couldn't do that I guess. The prototype would be template<class T> operator%(format& , const T& ) now a call: format("...") % 1 would fail, as format("...") is an rvalue, and rvalues cannot be bind to non-const references (except as the this argument). The reason why the compiler prefers format % over lambda % is that in the match to lambda %, const is added to the format argument, whereas in format % it is not. Probably leaving things as they are is the best solution, there's doesn't seem to be much one can do for this. Best, Jaakko
-- Samuel
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (4)
-
Jaakko Jarvi
-
Jaakko Järvi
-
Samuel Krempp
-
Shawn Church