constness for arrays
David Medlock
noone at nowhere.com
Tue Jul 18 06:17:51 PDT 2006
xs0 wrote:
> As has been discussed, the lack of something like C++ const hurts most
> when using arrays, as you can't code around it like with classes,
> structs or primitives (with the latter two you can just pass by value,
> for classes you can make readonly versions). The fact that inbuilt
> strings are also arrays makes the problem occur often.
>
> I was wondering whether the following would resolve that issue:
>
> - the top bit of arrays' .length becomes an indicator of the
> readonlyness of the array reference
>
<snip>
I like it except it drops the max array size by half, doesn't it?
Since we are talking about dynamic arrays here, why not just:
1. add a flags byte or short to the internal array structure to hold it.
2. make the pointers lower bit hold it- this of course assumes the
pointer is at least word-aligned. I am not sure if this would conflict
with structs which are align(1).
-DavidM
More information about the Digitalmars-d
mailing list