Avoiding const?

BLM blm768 at gmail.com
Tue Feb 21 17:25:12 PST 2012


If you have a const array, you can create a non-const copy of the array using the
.dup property of the array. The reason that you need this is that dynamic-length
arrays share data when you assign them between variables, and you can't have a
non-const variable using something else's const data without running into some
really nasty problems.

In your specific case, though, the .dup property might not convert the inner
levels of the nested array. If it doesn't work, you could try something like this:

char[][] words;
words.length = set.keys.length;
foreach(size_t i, const char[] text; set.keys) {
 words[i] = text.dup;
}

If you don't really need to modify the individual characters in the array, you
might just want to stick with const; it will be more efficient. You might also
want to define the set as bool[string] because associative arrays prefer
const/immutable keys for some reason.


More information about the Digitalmars-d-learn mailing list