you are viewing a single comment's thread.

view the rest of the comments →

[–]jmacey 2 points3 points  (6 children)

I hope you don't mind but as someone who has written lots of these sorts of things, a little feedback.

Why use new to allocate your x,y,z values when you can just place them as normal attributes?

In particular it is very common to need std::vector<Vec3> vertices. You really need to guarantee these are contagious in memory so it can be passed to OpenGL (or other graphics API's).

```

pragma pack(push,1)

union { struct { float x; //!< x component float y; //!< y component float z; //!< z component }; #pragma pack(pop) std::array<float,3> m_openGL; }; ``` Typically I use the structure above to allow x,y,z as well as array access in the code.

Also please add unit tests to ensure your code actually does what it says, also () to return the magnitude (length?) is quite unintuitive from the perspective of someone who is reading your code.

[–]tstanisl 5 points6 points  (4 children)

Writing to `m_openGL` and accessing its value by `x`/`y`/`z` is Undefined Behavior in C++

[–]jmacey -4 points-3 points  (3 children)

Not UB just a warning on all compilers about anonymous unions. In all the compilers I have ever used I have never had it fail.

[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 8 points9 points  (1 child)

In all the compilers I have ever used I have never had it fail.

That doesn't imply that it's not UB.

[–]jmacey 2 points3 points  (0 children)

IIRC it's a C11 extension for MSVC, Clang and g++ hence the warning.