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:
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;
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.
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;
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
inline operator const vec4 () const {
return (vec4)*this;
}
does not work, and calling glMultMatrixf(mat4(...)); (for example) produces the same error message as without the operator.
defining operator float* in mat4 is of course impossible, since that will eliminate the ability to use [][] (ambigious operators)
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.