Is it possible to specify the address returned by the address of operator?
nkm1
t4nk074 at openmailbox.org
Wed Sep 27 19:55:07 UTC 2017
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.
More information about the Digitalmars-d-learn
mailing list