ultimaille
ultimaille copied to clipboard
code-review vec2 and vec3, are there performance issues for operator[] ?
Current implementation is entirely standard compliant; however accessing vec.z needs two comparisons.
Alternative A
glm-style return (&x)[i];
https://github.com/Groovounet/glm/blob/e1afbc9ceaacbc9aed7bb896d26c0872f8a2bf29/glm/core/type_vec3.inl#L49
vec3 can be converted to "plain old data" (POD) struct (need to remove custom constructors though). Conditions to qualify for POD:
- All data members are public and themselves POD or fundamental types (but not reference or pointer-to-member types), or arrays of such
- It has no user-defined constructors, assignment operators or destructors
- It has no virtual functions
- It has no base classes
Problem: padding bytes.
Alternative B
Another option is union. Technically it is UB, but fine in most compilers.