string initialization question.

Jonathan M Davis jmdavisprog at gmail.com
Fri Jul 30 12:56:36 PDT 2010


On Friday, July 30, 2010 10:14:45 Steven Schveighoffer wrote:
> I think a function to do it is fine, like makeArray('-', 5);

Well, creating a function for producing an array literal and returning it using 
templates and string mixins wouldn't be all that hard, but if you want to create 
a dynamic array of arbitrary size at runtime, that's not going to work, and a 
makeArray() function would have exactly the same tools that you have to create 
an array of all the same value. So, it's not going to be any more efficient that 
what you can do.

int[] a = new int[](x);
a[] = val;

_should_ be fairly easily optimized by the compiler and thus really should be 
optimized down to an initialization rather than an initialization and an 
assignment.

A makeArray() function wouldn't hurt any, but I don't think that it would really 
buy us much. Of course, truth be told, I've always thought that the ability to 
construct a string or vector in C++ all of a single value was pretty useless. 
Obviously, some people find it useful at least once in a while, but I've never 
had much use for it. A makeArray() function would probably still be a good thing 
to have, but what we really need here is either a syntactic way to do it or for 
the compiler to optimize out the useless initialization (as well as inline 
makeArray()) so that you don't have to deal with the extra cost of setting 
everything twice.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list