The Status of Const

Mafi mafi at example.org
Tue Aug 17 04:48:49 PDT 2010


Am 17.08.2010 12:05, schrieb Pelle:
> On 08/17/2010 01:44 AM, Steven Schveighoffer wrote:
>> On Mon, 16 Aug 2010 19:06:56 -0400, Simen kjaeraas
>> <simen.kjaras at gmail.com> wrote:
>>> Uhm, it works in D:
>>>
>>> class C {}
>>> class D : C {}
>>>
>>> void main( ) {
>>> D[] a;
>>> C[] b = a;
>>> }
>>
>> Yes, I was about to say that except that is a bug kind of. The type
>> system should only allow casting D[] to const(C)[].
>
> class C { }
> class D : C { }
> class E : C { }
>
> void append_to(ref const(C)[] cs, const(C) c) { cs ~= c; }
>
> D[] ds;
> append_to(ds, new E);
>
>
> *ahem*
> D[] can not be converted to const(C). That it works today is pretty
> terrible. Rewrite the append_to to work with Objects, and well. :-)

Appending something could reallocate so it's not available through ds. 
Even when it allocates in place the length of ds is *not* affected so 
you are not able to fetch the E instance through ds with bounds checking 
on.
	foreach(d; ds) {...}  // works also without bounds-checking
won't get the E instance.
When bounds-checking is off indexing is kind of pointer arithmethic 
which is unsafe anyways.

Mafi


More information about the Digitalmars-d mailing list