Is it possible to specify the address returned by the address of operator?

Jesse Phillips Jesse.K.Phillips+D at gmail.com
Wed Sep 27 21:01:36 UTC 2017


On Wednesday, 27 September 2017 at 16:35:54 UTC, DreadKyller 
wrote:
> My question is about overloading, several operators can be 
> overloaded in D, one of the ones that can't apparently is the 
> address of operator (&object). My question is have I simply 
> missed it or does it actually not exist, and if it's not 
> overloadable, is there any reason why this was decided? Because 
> there's been numerous times that it'd be useful to me, just 
> recently with how much I use the operator because of OpenGL I 
> decided to ask.

My answer is that & is a defined operation on all addressable 
memory. Unlike other operators which don't exist until you 
"overload" them.

For example, if you store your Matrix in a custom container it 
could try to store pointer rather than the struct itself, if & is 
overloaded the generic implementation would be broken because it 
would no longer be a pointer to Matrix but to the inner element.

Whereas generic code which utilizes addition or append can assume 
the type appropriately defined the behavior to semantically match 
the desired use, generic code would be broken if the type changed 
& to do something different from what the language defines it to 
do.


More information about the Digitalmars-d-learn mailing list