Inserting to associative array do not return a lvalue.

Bill Baxter dnewsgroup at billbaxter.com
Tue Oct 16 02:03:09 PDT 2007


Yang Bo wrote:
> Sorry. I thought I have a mistake. Holding a pointer to the value of an
> associative array is very dangerious.Yang Bo 写道:
>> I tend to write code like this:
>>
>> int[int] a;
>> int* r = &(a[1] = 2); // error
>> (*r)++;
>>
>> The problem is, `a[1] = 2` does not return a lvalue. I must write:
>>
>> int[int] a;
>> a[1] = 2;
>> int* r = a[1];
>> (*r)++;
>>
>> I have to look up the associative array twice, this is unwanted cost.

If you really want to be able to do things like that, there are Map
container classes out there that will allow it.  But modifying pointers
to _keys_ is always a no-no.

Some places you can find a Map class:
  Tango, Arclib, OpenMesh/D, Juno, Stewart's Utility Library

--bb



More information about the Digitalmars-d mailing list