Re: ostream_iterator with namespaced types - BUG?
From: Andy Coates (n0_chance_at_n0_where.com)
Date: 10/15/04
- Next message: Jerry J: "erasing item while looping through a vector"
- Previous message: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- In reply to: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- Next in thread: Andy Coates: "Re: ostream_iterator with namespaced types - BUG?"
- Reply: Andy Coates: "Re: ostream_iterator with namespaced types - BUG?"
- Reply: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Jerry J: "erasing item while looping through a vector"
- Previous message: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- In reply to: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- Next in thread: Andy Coates: "Re: ostream_iterator with namespaced types - BUG?"
- Reply: Andy Coates: "Re: ostream_iterator with namespaced types - BUG?"
- Reply: Igor Tandetnik: "Re: ostream_iterator with namespaced types - BUG?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|