const?? When and why? This is ugly!

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat Mar 7 13:46:24 PST 2009


grauzone wrote:
> Walter Bright wrote:
>> Burton Radons wrote:
>>> That's what we said about strings in 1.0. You modify it, you copy it,
>>> or you tell the user. The gentleman's agreement worked perfectly and
>>> that came without a mess of keywords, without implicit or explicit
>>> restrictions on behaviour, without having to condition templates.
>>
>> The one flaw in it was the behavior I consistently saw of "I'm copying 
>> the string just to be sure I own it and nobody else changes it." D was 
>> meant for copy-on-write, which means copy the string *only* if you 
>> change it. No defensive copying. No "just in case" copying. The 
>> gentleman's agreement failed as far as I could tell.
>>
>> With immutable strings, the gentleman's agreement is enforced.
> 
> What about automatic, built-in copy on write?

No go with threads. COW sounded like a great idea for std::string in 
ancient times when threads were a rarity. Today, virtually all C++ 
implementations actively dropped COW and replaced it with eager copy + 
small string optimization for short strings. D really has the best of 
all worlds solution.

Andrei



More information about the Digitalmars-d mailing list