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

DreadKyller dreadkyller at gmail.com
Wed Sep 27 20:24:24 UTC 2017


On Wednesday, 27 September 2017 at 19:55:07 UTC, nkm1 wrote:
> On Wednesday, 27 September 2017 at 16:35:54 UTC, DreadKyller 
> wrote:
>> Been using D for a couple years now, however one problem I've 
>> had, more so recently since I've been dealing a lot with 
>> OpenGL is related to pointers.
>>
>> I have a matrix object to aid with the matrix math required 
>> for working with 3D transforms. However OpenGL (I'm using 
>> DerelictGL3 bindings) requires pointers to the data. I am 
>> currently doing the following:
>>
>> Matrix!float ortho(float l, float r, float b, float t, float 
>> f, float n = -1)
>> {
>> 	Matrix!float oMat = identity(); // Get default Identity Matrix
>> 	
>> 	oMat[0,0] =  2 / (r - l);
>> 	oMat[1,1] =  2 / (t - b);
>> 	oMat[2,2] = -2 / (f - n);
>> 	
>> 	oMat[3] = [-(r+l)/(r-l), -(t+b)/(t-b), -(f+n)/(f-n), 1];
>> 	
>> 	return oMat;
>> }
>>
>> And then to use with OpenGL (passing as uniform into shader):
>>
>> glUniformMatrix4fv(transform_uniform, 1, GL_FALSE, matrix.addr 
>> );
>>
>> where addr is a property that returns the address of the first 
>> item in the Matrix's internal data. I know I can also use 
>> &matrix[0][0]
>>
>> 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.
>
> & is not overloadable, presumably because some people were 
> annoyed by abuse of operator overloading in C++. The reason is 
> to improve readability (of other people's code).
> Just rename matrix.addr to matrix.ptr... like in arrays: 
> https://dlang.org/spec/arrays.html#array-properties
> That would be clearer (opinion), since the reader of your code 
> can assume that matrix.ptr does the same thing with your matrix 
> as array.ptr does with arrays. OTOH, overloading &matrix to do 
> something different from built in &matrix seems like a 
> pointless obfuscation to me.

I mean to an extent I agree, but I moved from using GLfloat 
arrays for matrix's to a Matrix object to aid in Matrix math like 
multiplication, so I have to go through my code and in many 
places change it to using matrix.ptr, considering that I have to 
do this for every object in a scene multiple times for the 
translation, scale, projection and etc matrix's it's more 
tedious, and I'm the only one working on the code and I comment 
and document my code a lot. If one thing annoys m e about 
language design, it's when peoples actions dictate the decisions 
of the language, it shouldn't be the responsibility of the 
language to limit users in order to prevent bad habits or 
practices, it should be the responsibility of education to do so. 
The attitude of "some people use this feature incorrectly, so 
let's ban it's use entirely" is honestly ridiculous to me, but oh 
well, that's apparently the modern philosophy.


More information about the Digitalmars-d-learn mailing list