Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive type casting
    text
    copied!<p>I got a typical 'vector4' class with an operator float* to autocast it for gl*4fv as well as []. There's also 'const' version for optimizations for the compiler as well as const refrences, and this works fine:</p> <pre><code>typedef struct vec4 { ... // ----------------------------------------------------------------- // // Cast operator, for [] inline operator float* () { return (float*)this; } // Const cast operator, for const [] inline operator const float* () const { return (const float*)this; } // ----------------------------------------------------------------- // ... // Vertex / Vector struct { float x, y, z, w; }; // Color struct { float r, g, b, a; }; } vec4; </code></pre> <p>My problem is when I now coded a 'matrix4' class, with operator vec4* which supports extracting rows from the matrix, and also have the 'side-effect' of having matrix[][] operator which is nice.</p> <pre><code>typedef struct mat4 { ... // ----------------------------------------------------------------- // // Cast operator, for [] inline operator vec4* () { return (vec4*)this; } // Const cast operator, for const [] inline operator const vec4* () const { return (const vec4*)this; } // ----------------------------------------------------------------- // private: float f[16]; } mat4; </code></pre> <p>My question is, why doesn't the compiler detect the ability to convert a mat4 to float*? I would suspect that the heritage of mat4 -> vec4 -> float* is reasonable, but it doesn't seem so. It came to my mind that the compiler might see it as mat4 -> vec4* -> float* which is not defined, but that assumption was invalid, since defining the operator</p> <pre><code>inline operator const vec4 () const { return (vec4)*this; } </code></pre> <p>does not work, and calling glMultMatrixf(mat4(...)); (for example) produces the same error message as without the operator.</p> <p>defining operator float* in mat4 is of course impossible, since that will eliminate the ability to use [][] (ambigious operators)</p> <p>Any solutions for this? or do I have to manually cast to vec4 everytime I want to autocast to float*? Auto-casting is a really nice feature and it interpolates the code with OpenGL neatly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload