Boost logo

Boost Users :

From: Jaakko Järvi (jajarvi_at_[hidden])
Date: 2004-01-29 16:30:34


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<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.
>
> 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_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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