In article <d6652001.0307180209.2d56a946@posting.google.com>,
on 18 Jul 2003 21:07:05 -0400,
Which is why some of us use for_each and a "smart¹" functor to do this
kind of thing:
struct print_it
{
******** print_it( std:

stream& out, const char *sep = "" )
: out_( out ), subsequent_( false ), sep_( sep ) {}
template<typename T>
void operator()( const T& output ) const
{
if( subsequent_ )
{
if( sep_ )
out_ << sep_;
}
else
{
subsequent_ = true;
}
out_ << output;
}
private:
mutable bool subsequent_; // Yes, it's a poor name...
std:

stream& out_;
const char * const sep_;
}; void fubar() {
std::list<int> x;
// do stuff
std::for_each( x.begin(), x.end(), print_it( std::cout, "," ) );
}
It's kind of ugly that print_it has mutable² state, but them's the
breaks. I'd be interested in hearing of any real-world implementations
of for_each that cause this to break. (I can imagine one, but it's a bit
pathological..)
Regards,
Andy S.
¹ For some value of smart...
² Mutable in both in the English language _and_ the C++ language senses

--
"Light thinks it travels faster than anything but it is wrong. No matter
how fast light travels it finds the darkness has always got there first,
and is waiting for it." -- Terry Pratchett, Reaper Man
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]