Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2008-07-14 09:08:35


on Mon Jul 14 2008, "Robert Jones" <robertgbjones-AT-gmail.com> wrote:

> On Fri, Jul 11, 2008 at 4:41 PM, David Abrahams <dave_at_[hidden]> wrote:
>
> Note that std::endl can cause you big troouble, but since you're using
> '\n' above I don't see a reason to worry. You do need something like
> "using namespace boost::lambda;" to make "_1" valid, though.
>
> Back in the office on Monday morning, and applying my new found
> knowledge my Boost-using code compiles perfectly, but for one little
> snag!
>
> For brevity and clarity I posted example code using '\n' line terminators.
> However my real code uses endl - which is indeed causing me big troouble!
>
> Are there any techniques for successfully using std::endl in lamda functions?

Not any good ones. The problem is that endl is a function template and

    ostream << std::endl;

matches the operator<< overload for stream manipulators, which are
function pointers, something like:

     ostream& operator<<( ostream, some-concrete-function-pointer-type );

so the compiler implicitly specializes std::endl<X...> so that it gets
the right function pointer. It's nasty.

I do this if I really, really, really want to write endl instead of '\n'
(not that I ever want that ;-> ):

     struct endl_
     {
         friend ostream& operator<<(ostream& x, endl_)
         {
             x << std::endl;
             return x;
         };
     } endl;

Then I'd just stream my own endl.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk