To dup or not to dup?

Johan Granberg lijat.meREM at OVE.gmail.com
Sun Feb 25 13:11:50 PST 2007


Jürgen Herz wrote:

> Hi,
> 
> I'm absolutely new to D and about to see if D is a language I could like.
> On http://www.digitalmars.com/d/cppstrings.html I found
> 
> char[] s1 = "hello world";
> char[] s2 = "goodbye      ".dup;
> s2[8..13] = s1[6..11];                // s2 is "goodbye world"
> 
> The .dup is needed because string literals are read-only in D, the .dup
> will create a copy that is writable.
> 
> The same with
> char []a = "Txst";
> a[1] = 'a';
> 
> I find that interesting because here it also works (compiles and runs)
> without .dup (DMD 1.007). Has the language changed since writing that
> web page?
> 
> Jürgen

You should not write to string literals but if the compiler enforces it or
not is a quality of implementation issue.



More information about the Digitalmars-d mailing list