|
Boost : |
From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-01-12 21:16:21
christopher diggins wrote:
> ----- Original Message -----
> From: "Jonathan Turkanis"
>> void ToUpper() {
>> char c;
>> while (cin.get(c)) cout.put(c);
>> }
>> I don't see why this is better than
>>
>> struct toupper_filter : input_filter {
>> template<typename Source>
>> int get(Source& src) { return toupper(boost::io::get(src)); }
>> };
>>
>> and its cousins.
>
> Then that pretty much closes the discussion. If you do not see the
> benefits of the lower complexity, then there really is little more I
> can do to convince you otherwise.
It's not that I don't see the benefit of lower complexity; I don't really see
the lower complexity. ;-)
The only noticeable difference is that mine contains "template<typename Source>"
and uses a non-member get function. Your version requires that an entire stream
of data be processed at once -- leading to poor memory use -- and doesn't work
at all for streams which have no natural end. To justify this I'd like to see
something more substantial that the elimination of "template<typename Source>".
>>> int main() {
>>> fstream("input.txt") > Filter(ToUpper);
>>> }
>>
>> This won't work, since fstream("input.txt") produces a temporary
>> object which
>> cannot be used to initialize a non-const reference. Therefore
>> operator< would
>> have to be declared to take a const reference, and the non-const
>> extraction
>> functions would not be usable.
>
> I already made it work by creating temporary helper objects which
> take the address of the temporary objects.
The Boost.Iostreams solution (if I add your extension to the |-syntax) is as
follows:
file("input.txt") | toupper_filter() | ref(std::cout);
This seems very elegant to me. I don't see any reason to make the reference to
std::cout implicit. In fact, I think it would make the above more difficult to
understand.
Jonathan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk