Fixed ImVec2 operator[] violating aliasing rules causing issue with Intel C++ compiler. (#6272)
Note that this is not BayesBug's exact intended solution, so issues would be my responsibility ;)
Amended.
diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index 45fe314..a079dba 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -54,6 +54,8 @@
- ColorEdit: Fixed shading of S/V triangle in Hue Wheel mode. (#5200, #6254) [@jamesthomasgriffin]
- Rendering: Using adaptative tesselation for: RadioButton, ColorEdit preview circles,
Windows Close and Collapse Buttons.
+- Misc: Fixed ImVec2 operator[] violating aliasing rules causing issue with Intel C++
+ compiler. (#6272) [@BayesBug]
- IO: Fixed support for calling io.AddXXXX functions fron inactive context (wrongly
advertised as supported in 1.89.4). (#6199, #6256, #5856) [@cfillion]
- Backends: OpenGL3: Fixed GL loader crash when GL_VERSION returns NULL. (#6154, #4445, #3530)
diff --git a/imgui.h b/imgui.h
index 72947b3..351826f 100644
--- a/imgui.h
+++ b/imgui.h
@@ -255,8 +255,8 @@
float x, y;
constexpr ImVec2() : x(0.0f), y(0.0f) { }
constexpr ImVec2(float _x, float _y) : x(_x), y(_y) { }
- float operator[] (size_t idx) const { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine.
- float& operator[] (size_t idx) { IM_ASSERT(idx == 0 || idx == 1); return (&x)[idx]; } // We very rarely use this [] operator, the assert overhead is fine.
+ float& operator[] (size_t idx) { IM_ASSERT(idx == 0 || idx == 1); return ((float*)(char*)this)[idx]; } // We very rarely use this [] operator, so the assert overhead is fine.
+ float operator[] (size_t idx) const { IM_ASSERT(idx == 0 || idx == 1); return ((const float*)(const char*)this)[idx]; }
#ifdef IM_VEC2_CLASS_EXTRA
IM_VEC2_CLASS_EXTRA // Define additional constructors and implicit cast operators in imconfig.h to convert back and forth between your math types and ImVec2.
#endif