Container hierarchy vs. container types

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri Mar 5 13:09:08 PST 2010


Michel Fortin wrote:
> On 2010-03-05 11:32:27 -0500, Jonathan M Davis <jmdavisProg at gmail.com> 
> said:
> 
>>> Why couldn't a container have the same interface as a range, only with
>>> the added ability to push elements into it?
>>
>> Probably because it would become way too easy to accidentally use the
>> container as a range, which in many cases would end up emptying it when
>> passing it to one of the std algorithms.
> 
> Yes, I understand that.
> 
> My point is that the problem already exists with ranges: by exposing a 
> stream as a range, you'll empty the stream when iterating over it. If 
> that's acceptable for streams, why is it not for containers?
> 
> And perhaps you actually want to empty the container as you iterate over 
> it. If you use your container as a queue, that's what you'll expect. In 
> fact, a stream is just like a queue, and you want algorithms to work on 
> streams don't you?

A range is a view on a container. As such, popping off a range does not 
affect the topology of the underlying container. Consider, for contrast, 
a balanced binary tree. Removing an element triggers a rebalance of the 
tree, but popping an element off an iterator in that tree does not 
affect the tree itself.

Fair enough?


Andrei



More information about the Digitalmars-d mailing list