Implicit dereferencing

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 1 21:48:54 PDT 2013


On Tue, 02 Apr 2013 00:12:37 -0400, Luís Marques <luismarques at gmail.com>  
wrote:

> On Tuesday, 2 April 2013 at 03:36:04 UTC, Steven Schveighoffer wrote:
>> Plus it would make pointers to indexed types inconsistent with all  
>> pointers to types that don't define indexing.
>
> Yeah, of course you're right...
>
> Taking a step back, I think the problem here is that I wanted to have  
> multiple references to the contents of an associative array. With a  
> regular array, while you can't *store* a reference to the array "header"  
> (ptr, length) without using pointers, you can have another array  
> pointing to the same content. I guess you can't do something like that  
> with an associative array, right? (mm... that kinda breaks the ideia of  
> an AA as a more general array)

An AA is a pImpl, meaning a pointer to implementation.  A simple copy is  
an alias.

HOWEVER, there is one horrible caveat.  You must have assigned an element  
at least once in order to alias:

int[int] aa1; // pImpl initialized to null
int[int] aa2; // pImpl initialized to null

aa2 = aa1; // just assigns null to aa2
aa1[1] = 1; // allocates and initializes
aa2[1] = 2; // allocates and initializes separate instance
assert(aa1[1] == 1 && aa2[1] == 2);
aa2 = aa1; // NOW they are aliased
assert(aa2[1] == 1);
aa2[1] = 2;
assert(aa1[1] == 2);

-Steve


More information about the Digitalmars-d mailing list