Re: A std::ostringstream wrapper for on-the-fly streaming - is this
dodgy?
On Aug 5, 12:46 am, Francesco <entul...@gmail.com> wrote:
On Aug 4, 6:36 pm, Stuart Golodetz
[snip]
I've done a test with my logger class.
Which wraps an ostream, I'd guess.
Among the following 32 STL manipulators:
-------
boolalpha, dec, endl, ends, fixed, flush, hex, internal, left,
noboolalpha, noshowbase, noshowpoint, noshowpos, noskipws, nounitbuf,
nouppercase, oct, resetiosflags, right, scientific, setbase, setfill,
setiosflags, setprecision, setw, showbase, showpoint, showpos, skipws,
unitbuf, uppercase, ws
-------
Only the last one (std::ws) doesn't get handled by the "operator<<
(std::ostream& (*)(std::ostream&))" function - actually, it accepted
also those manipulators that make no sense with ostreams, that would
be "std::skipws" and "std::noskipws", but that seems OK, according tohttp=
://www.cplusplus.com/reference/iostream/manipulators/
On the other hand, the "operator<<(std::ios& (*f)(std::ios&))"
function did not accept "std::ws" either.
That's normal, since std::ws (instantiated for char) is:
std::istream& ws( std::istream& ) ;
The ws manipulator only works on istream's, since it actually
reads characters. The other manipulators manipulate fields in
std::ios_base, fields which are present in both istream and
ostream, even if some of them are only used in one of these.
(In fact, most of them are only used in ostream. But skipws is
only used in istream.)
I'm surprised that most of these manipulators worked with
"operator<<( std::ostream& (*)( std::ostream& )". According to
the standard, they have a signature of:
std::ios_base& manipulator( std::ios_base& )
and shouldn't match that function. Are you sure they aren't
being handled by "operator<<( std::ios& (*)( std::ios& ) )"?
The "operator<<( std::ostream& (*)( std::ostream& ) )" is there
for the manipulators which only work with ostreams: endl, ends
and flush; all of the others should use the ios version.
I'm going to do further testing anyway - for the moment I
didn't check if the manipulators actually affected the stream,
I've only checked if they were accepted by the << operator.
I've used setw extensively with such wrappers, as well as
instances of my own manipulators. (Other than setw and endl, I
can't imagine actually using one of the standard manipulators
directly.)
I've been able to compile the "operator<<(T& (*manip)(T&))"
function but it wasn't able to accept any manipulator at all.
The manipulator functions are templates (they work on any
basic_ios or basic_ostream/basic_istream). In order for the
above to be called, template argument deduction has to be able
to figure out the type of T. Passing it a function template
doesn't allow this.
Currently, there seems to be no need for any function other
than the "operator<<(T t)" template along with
"operator<<(std::ostream& (*) (std::ostream&))", at least for
my very own needs.
I'm pretty sure you're missing something. You really do need
the "operator<<( std::ios& (*)( std::ios& ) )" as well.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34