Boost logo

Boost Users :

Subject: Re: [Boost-users] begin (cout) or boost::copy to cout
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2011-09-13 20:41:18


> 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.

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.

Regards,
Nate
                                               



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net