Assigning to char[N]

Ali Çehreli acehreli at yahoo.com
Wed Feb 1 21:36:02 PST 2012


On 02/01/2012 04:34 PM, bearophile wrote:
 > Ali:
 >
 >>       a["one".length .. $] = '\0';
 >
 > Is DMD able to optimize that string literal away? (Some assembly may 
be required).

I sure hope so. "one".length should be a compile time constant.

 >>       // copy(a, "new content");
 >>       // fill(a, "new content");
 >>       // insertInPlace(a, "new content");
 >>       //
 >>       // Those do not work with errors similar to this:
 >>       //
 >>       //   Error: template std.algorithm.copy(Range1,Range2) if
 >>       //   (isInputRange!(Range1)&&
 >>       //   isOutputRange!(Range2,ElementType!(Range1))) does not 
match any
 >>       //   function template declaration
 >
 > Try harder. Try to slice that a.
 >
 > Bye,
 > bearophile

First of all, the parameters in my copy() call were swapped above. It 
should have been source then destination:

   copy("new content", a);

But it won't work, because strings are ranges of Unicode code points 
(i.e. dchar ranges). Even narrow strings are exposed as dchar ranges. So 
the source is an InputRange in that call, and the destination is an 
output range, but their element types don't match.

The following works because now all of the elements are dchar:

     dchar[100] a = '\0';
     copy("new content"d, a[]);

Ali



More information about the Digitalmars-d-learn mailing list