Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2003-09-09 22:54:58


On Wed, Sep 10, 2003 at 05:30:47AM +0200, Samuel Krempp wrote:
> le Samedi 6 Septembre 2003 10:24, gennadiy.rozental_at_[hidden] ?crivit :
> > Library add endl itself at the end of log entry. To add an extra empty
> > line you could use '\n'. I am not sure why endl failing, but it seems
> > pointless to use it.
>
> it fails for other manipulators too.
> that's because you cant pass functions by const reference.

Is this the real reason? (see below)

> It could be made to work :
>
> 1. either with a T const& / T& overload :
>
> template <class T>
> inline wrap_stringstream&
> operator<<( wrap_stringstream& targ, T const& t )
> {
> targ.stream() << t;
> return targ;
> }
>
> template <class T>
> inline wrap_stringstream&
> operator<<( wrap_stringstream& targ, T& t )
> { // same code..
>
> but this kind of overload is not supported by compilers like MSVC6.

Does this work? (I confess I have not tried it.) I thought the real
reason for the manipulator failure had to do with the fact that
   std::endl
is not the name of a C++ object. Rather it's the name of a template
function, and obscure C++ type system rules allow such a name to be
coerced into a pointer to a single instance of the template function,
provided there is sufficient external context information available.

I have run into this problem in other contexts, and I have never been
able to capture "endl" using a "template <class T>" parameter.

> 2. or by overloading it for the 3 following types to catch all 3 kinds of
> manipulators :
>
> ios_base& (*pfn)(ios_base&)
> basic_ostream<Elem,Tr> & (*pfn)(basic_ostream<Elem, Tr>&)
> basic_ios<Elem, Tr>& (*pfn)(basic_ios<Elem, Tr>&)
>
> (just like std::basic_ostream :: operator<< 's overloads)

I think this is the only way that truly works.

-- 
-Brian McNamara (lorgon_at_[hidden])

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