Re: ostream_iterator with namespaced types - BUG?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Andy Coates (n0_chance_at_n0_where.com)
Date: 10/15/04


Date: Fri, 15 Oct 2004 16:29:46 +0100


"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:OVugiissEHA.316@TK2MSFTNGP11.phx.gbl...
> "Andy Coates" <n0_chance@n0_where.com> wrote in message
> news:uyB%23iWssEHA.3580@TK2MSFTNGP10.phx.gbl
> > So, changing the subject now that we are talking about streams...
> >
> > This doesn't work either:
> >
> > class Stream;
> > Stream& bar(Stream& s);
> >
> > class Stream : public std::wostringstream
> > {
> > public:
> > void foo()
> > {
> > ((std::wostringstream&)*this) << bar;
> > }
> >
> > void works(){}
> > };
> >
> > Stream& bar(Stream& s)
> > {
> > s.works();
> > return s;
> > }
> >
> > Stream& operator<<(Stream& s, Stream& (__cdecl *pFn)(Stream&))
> > {
> > (*pFn)(s);
> > return s;
> > }
> >
> > Because, again, it matches the void* inserter.
>
> But of course. The first parameter of operator<< in your call is
> std::wostringstream&, not Stream, so your operator taking a Stream& does
> not have a chance. Remove the cast, forward-declare your operator<<()
> before the definition of Stream, and everything works as expected on my
> VC7.1
>
> Note that deriving from standard stream classes and creating your own
> versions of operator<< is not a very good idea. Chaining breaks.
> Consider:
>
> Stream s;
> s << bar; // this works
> s << 1 << bar; // this does not
>
> The first operator<<(ostream&, int) returns an ostream&, so then you
> effectively have
>
> (ostream&)s << bar;
>
> which we already know does not do what you want.
>
> > Is there a known, safe, solution to this kind of issue? Sorry if
> > this is a bit of a muppet question - I'm a competent C++ dev, just
> > never really tried deriving my own stream class before!
>
> Stream classes were not designed for being derived from. What are you
> trying to achieve?
> --
> With best wishes,
> Igor Tandetnik
>
> "On two occasions, I have been asked [by members of Parliament], 'Pray,
> Mr. Babbage, if you put into the machine wrong figures, will the right
> answers come out?' I am not able to rightly apprehend the kind of
> confusion of ideas that could provoke such a question." -- Charles
> Babbage
>
>

Sorry - the cast was not actually in my real code, just in my example by
mistake - and if I remove the cast to give:

    void foo()
     {
          (*this) << bar;
     }

it doesn't work on my .Net 2003. The << bar just calls the void* version
again.

What am I trying to achieve... I have several collections of structures (the
structures are defined in IDL). Each structure contains a collection of
strings and some other parameters. I need to iterate over each structure,
use the 'other' parameters to get some information and that, along with the
string collection, to create a large SQL string. My design uses a
customised stream class to hold the 'other' information and automatically
handle the formatting when the strings are inserted. It also provides a
load of operators (a long the lines of std::hex and std::setfill) to handle
the custom cases.

I guess I'll have to change this to pass the stream and the 'other'
information as too seperate parameters to a method, rather than using a
custom stream and operator<<s. It's a shame because the custom stream class
allowed me to hide the stream handling side of things away in another class.

Andy



Relevant Pages

  • Re: Character semantics for filenames (was: win32 reading wide filenames (unicode))
    ... DO WITH CHARACTERS ABOVE "\xFF". ... suspect, openworks on the supplied byte stream AS IS, discregarding ... Unocode inserts hints in strings. ... encoding to perl strings by readdir and from perl strings to the OS ...
    (comp.lang.perl.misc)
  • Re: XML::LibXML UTF-8 toString() -vs- nodeValue()
    ... Each character is a 32-bit value. ... You can do Regexps on *strings*, whether they are binary or text. ... You don't need regexps at all to parse XML. ... read the next block or line from the stream and append it to your ...
    (comp.lang.perl.misc)
  • Re: Optimizing a text statistics function
    ... I'd create one stream which reads through the file, ... to use strings which is less than ideal because they are immutable (I ... characters once they have been read from the StringBuffer) and therefore ... Neil Benn ...
    (comp.lang.python)
  • Re: streams, etc
    ... | connection between strings and streams is murky to me. ... as opposed to the extraction operator '>>', ... are extracting data from the stream. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Reading from a stream til EOF
    ... > stuff is about doing optimizations in a centralized form - however, ... stream buffers do two things: ... And reading strings is just another example: ...
    (comp.lang.cpp)