Why I (Still) Won't Use D

Walter Bright newshound1 at digitalmars.com
Fri Mar 28 02:38:26 PDT 2008


Janice Caron wrote:
> On 28/03/2008, Walter Bright <newshound1 at digitalmars.com> wrote:
>> Yet I'm always left wondering what is the difference between vector and
>> string?
> 
> In Microsoft Visual Studio's implementation, std::string implements
> copy-on-write, wheras std::vector doesn't. e.g.
> 
>     std::vector<char> v1 = whatever;
>     std::vector<char> v2 = v1; // makes a copy
> 
>     std::string s1 = whatever;
>     std::string s2 = s1; // no copy made, YET
> 
>     s2[0] = 'x'; // NOW a copy is made
> 
> This is purely an implementation difference. It is not specified in
> the standard.
> 
> D takes a sort of halfway-in-between approach. In D, we copy arrays
> (including strings) by reference, and implement copy-on-write in the
> algorithms; In MSVC++ one copies vectors and string by value, and
> copy-on-write is implemented in the design of std::string.

Making strings arrays of invariant makes them implicitly copy-on-write. 
More generally, whether D arrays are copy-on-write or not is based on 
whether the element type is invariant or mutable. It isn't based on if 
the contents are text or not.



More information about the Digitalmars-d mailing list