AMDG On 09/13/2011 05:41 PM, Nathan Ridge wrote:
This function eliminats the need of passing explicit template parameters to std:: ostream_iterator, which makes the code fragile and hard to read.
I agree that having to pass a template parameter to std::ostream_iterator is annoying.
However, overloading boost::begin for std::ostream is not a good solution, because, as Steven pointed out, ostream is not a range. (And certainly, overloading boost:copy for something as specific as std::ostream is not a good solution - it goes directly against the spirit of generic programming, which is to express algorithms in terms of a generic interface (iterators), and have data structures adapt to that interface).
A better solution is to write a polymorphic version of ostream_iterator; that is, a version that's not a template, but which has a templated operator=, so that you can output any type using the iterator.
I suspect that it would be difficult to make a valid STL iterator like that. How would you define std::iterator_traits for it?
With such a modified ostream_iterator, you can write:
boost:copy(some_range, my_ostream_iterator(cout, " "));
which is very close to what you are aiming for.
In Christ, Steven Watanabe