Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Index of this file:
// [SECTION] Dear ImGui end-user API functions
// [SECTION] Flags & Enumerations
// [SECTION] Tables API flags and structures (ImGuiTableFlags, ImGuiTableColumnFlags, ImGuiTableRowFlags, ImGuiTableBgTarget, ImGuiTableSortSpecs, ImGuiTableColumnSortSpecs)
// [SECTION] Helpers: Debug log, Memory allocations macros, ImVector<>
// [SECTION] Helpers: Debug log, Memory allocations macros, ImVector<>, Unreachable
// [SECTION] ImGuiStyle
// [SECTION] ImGuiIO
// [SECTION] Misc data structures (ImGuiInputTextCallbackData, ImGuiSizeCallbackData, ImGuiPayload)
Expand Down Expand Up @@ -287,6 +287,7 @@ typedef int (*ImGuiInputTextCallback)(ImGuiInputTextCallbackData* data);
typedef void (*ImGuiSizeCallback)(ImGuiSizeCallbackData* data); // Callback function for ImGui::SetNextWindowSizeConstraints()
typedef void* (*ImGuiMemAllocFunc)(size_t sz, void* user_data); // Function signature for ImGui::SetAllocatorFunctions()
typedef void (*ImGuiMemFreeFunc)(void* ptr, void* user_data); // Function signature for ImGui::SetAllocatorFunctions()
namespace ImGui { [[noreturn]] inline void Unreachable(); }; // Function signature for ImGui::Unreachable()

// ImVec2: 2D vector used to store positions, sizes etc. [Compile-time configurable type]
// - This is a frequently used type in the API. Consider using IM_VEC2_CLASS_EXTRA to create implicit cast from/to our preferred type.
Expand All @@ -297,8 +298,15 @@ struct ImVec2
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) { IM_ASSERT(idx == 0 || idx == 1); return ((float*)(void*)(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 void*)(const char*)this)[idx]; }
Copy link
Contributor

@achabense achabense Dec 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though access from reinterpreted this is truely UB... I think it's enough to keep the old assertion and just return idx == 0 ? x : y.

#define SUBSCRIPT_OPERATION \
switch (idx % 2) { \
case 0: return x; \
case 1: return y; \
default: ImGui::Unreachable(); /* Compiler hint: ensures optimization is taken and removes a false positive non-returning branch. */ \
};
float& operator[] (size_t idx) { SUBSCRIPT_OPERATION; }
float operator[] (size_t idx) const { SUBSCRIPT_OPERATION; }
#undef SUBSCRIPT_OPERATION
#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
Expand Down Expand Up @@ -2150,7 +2158,7 @@ struct ImGuiTableColumnSortSpecs
};

//-----------------------------------------------------------------------------
// [SECTION] Helpers: Debug log, memory allocations macros, ImVector<>
// [SECTION] Helpers: Debug log, memory allocations macros, ImVector<>, Unreachable
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -2255,6 +2263,37 @@ struct ImVector
};
IM_MSVC_RUNTIME_CHECKS_RESTORE

//-----------------------------------------------------------------------------
// ImGui::Unreachable()
// Provides an equivalent for std::unreachable() for C++<23. This signals to the compiler that a region is unreachable, allowing optimizations.
//-----------------------------------------------------------------------------
// If you are using C++>=23, use std::unreachable() in <utility>, and you may also define STD_UNREACHABLE_IS_AVAILABLE to substitute definitions.
// Code is from: https://en.cppreference.com/w/cpp/utility/unreachable.html
// Inclusion in the ImGUI namespace is chosen to avoid possible namespace collisions.
//-----------------------------------------------------------------------------
namespace ImGui
{
//#define STD_UNREACHABLE_IS_AVAILABLE
#ifdef STD_UNREACHABLE_IS_AVAILABLE
#include <utility>
constexpr const auto Unreachable = ::std::unreachable;
#undef STD_UNREACHABLE_IS_AVAILABLE
#else
[[noreturn]] inline void Unreachable()
{
// Uses compiler specific extensions if possible.
// Even if no extension is used, undefined behavior is still raised by
// an empty function body and the noreturn attribute.
#if defined(_MSC_VER) && !defined(__clang__) // MSVC
__assume(false);
#else // GCC, Clang
__builtin_unreachable();
#endif
}
#endif

} // namespace ImGui

//-----------------------------------------------------------------------------
// [SECTION] ImGuiStyle
//-----------------------------------------------------------------------------
Expand Down