std.algorithm.remove and principle of least astonishment

Rainer Deyke rainerd at eldwood.com
Sat Nov 20 19:42:32 PST 2010


On 11/20/2010 16:58, Andrei Alexandrescu wrote:
> On 11/20/10 12:32 PM, Rainer Deyke wrote:
>> std::vector<bool>  in C++ is a specialization of std::vector that packs
>> eight booleans into a byte instead of storing each element separately.
>> It doesn't behave exactly like other std::vectors and technically
>> doesn't meet the C++ requirements of a container, although it tries to
>> come as close as possible.  This means that any code that uses
>> std::vector<bool>  needs to be extra careful to take those differences in
>> account.  This is especially an issue when dealing with generic code
>> that uses std::vector<T>, where T may or may not be bool.
>>
>> The issue with Vector!char is similar.  Because char[] is not a true
>> array, generic code that uses T[] can unexpectedly fail when T is char.
>>   Other containers of char behave like normal containers, iterating over
>> individual chars.  char[] iterates over dchars.  Vector!char can,
>> depending on its implementation, iterate over chars, iterate over
>> dchars, or fail to compile at all when instantiated with T=char.  It's
>> not even clear which of these is the correct behavior.
> 
> The parallel does not stand scrutiny. The problem with vector<bool> in
> C++ is that it implements no formal abstraction, although it is a
> specialization of one.

The problem with std::vector<bool> is that it pretends to be a
std::vector, but isn't.  If it was called dynamic_bitset instead, nobody
would have complained.  char[] has exactly the same problem.

>> Vector!char is just an example. Any generic code that uses T[] can
>> unexpectedly fail to compile or behave incorrectly used when T=char.
>> If I were to use D2 in its present state, I would try to avoid both
>> char/wchar and arrays as much as possible in order to avoid this
>> trap. This would mean avoiding large parts of Phobos, and providing
>> safe wrappers around the rest.
> 
> It may be wise in fact to start using D2 and make criticism grounded in
> reality that could help us improve the state of affairs.

Sorry, but no.  It would take a huge investment of time and effort on my
part to switch from C++ to D.  I'm not going to make that leap without
looking first, and I'm not going to make it when I can see that I'm
about to jump into a spike pit.

> The above is
> only fallacious presupposition. Algorithms in Phobos are abstracted on
> the formal range interface, and as such you won't be exposed to risks
> when using them with strings.

I'm not concerned about algorithms, I'm concerned about code that uses
arrays directly.  Like my Vector!char example, which I see you still
haven't addressed.


-- 
Rainer Deyke - rainerd at eldwood.com


More information about the Digitalmars-d mailing list