From f3dd5566c659e3ef8776cba4a4ec115ca7acb09f Mon Sep 17 00:00:00 2001 From: Sharadh Rajaraman Date: Mon, 28 Sep 2026 16:18:32 +0100 Subject: [PATCH] Add constexpr alternatives to public macros Macros can't be exported from a C++20 module, and they ignore scope and type in any case. Add typed constexpr equivalents for the values people commonly use: version, payload types, unicode limits, colour packing, IM_COUNTOF() and IMGUI_CHECKVERSION(). Each is defined from its macro, so the two always agree, including when imconfig.h overrides them. They need C++17 inline variables, so they sit behind IMGUI_HAS_CONSTEXPR and C++11 builds are unaffected. --- imgui.h | 40 ++++++++++++++++++++++++++++++++++++++++ imgui_macros.h | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/imgui.h b/imgui.h index 8d3587aa4..7b7cccd9b 100644 --- a/imgui.h +++ b/imgui.h @@ -47,6 +47,7 @@ Index of this file: // [SECTION] Font API (ImFontConfig, ImFontGlyph, ImFontGlyphRangesBuilder, ImFontAtlasFlags, ImFontAtlas, ImFontBaked, ImFont) // [SECTION] Viewports (ImGuiViewportFlags, ImGuiViewport) // [SECTION] ImGuiPlatformIO + other Platform Dependent Interfaces (ImGuiPlatformImeData) +// [SECTION] Constexpr alternatives to macros // [SECTION] Obsolete functions and types */ @@ -4010,6 +4011,45 @@ struct ImGuiPlatformImeData ImGuiPlatformImeData() { memset((void*)this, 0, sizeof(*this)); } }; +//----------------------------------------------------------------------------- +// [SECTION] Constexpr alternatives to macros +// (requires C++17: see IMGUI_HAS_CONSTEXPR in imgui_macros.h) +//----------------------------------------------------------------------------- + +#ifdef IMGUI_HAS_CONSTEXPR +// Version (= IMGUI_VERSION, IMGUI_VERSION_NUM) +inline constexpr char ImGuiVersion[] = IMGUI_VERSION; +inline constexpr int ImGuiVersionNum = IMGUI_VERSION_NUM; + +// Standard Drag and Drop payload types (= IMGUI_PAYLOAD_TYPE_COLOR_3F, IMGUI_PAYLOAD_TYPE_COLOR_4F) +inline constexpr char ImGuiPayloadType_Color3F[] = IMGUI_PAYLOAD_TYPE_COLOR_3F; +inline constexpr char ImGuiPayloadType_Color4F[] = IMGUI_PAYLOAD_TYPE_COLOR_4F; + +// Unicode (= IM_UNICODE_CODEPOINT_INVALID, IM_UNICODE_CODEPOINT_MAX) +inline constexpr ImWchar32 ImUnicodeCodepoint_Invalid = IM_UNICODE_CODEPOINT_INVALID; +inline constexpr ImWchar32 ImUnicodeCodepoint_Max = IM_UNICODE_CODEPOINT_MAX; + +// 32-bit encoded colors (= IM_COL32(), IM_COL32_WHITE, IM_COL32_BLACK, IM_COL32_BLACK_TRANS, IM_COL32_X_SHIFT, IM_COL32_A_MASK) +constexpr ImU32 ImCol32(ImU32 r, ImU32 g, ImU32 b, ImU32 a) { return IM_COL32(r, g, b, a); } +inline constexpr ImU32 ImCol32_White = IM_COL32_WHITE; +inline constexpr ImU32 ImCol32_Black = IM_COL32_BLACK; +inline constexpr ImU32 ImCol32_BlackTrans = IM_COL32_BLACK_TRANS; +inline constexpr int ImCol32_RShift = IM_COL32_R_SHIFT; +inline constexpr int ImCol32_GShift = IM_COL32_G_SHIFT; +inline constexpr int ImCol32_BShift = IM_COL32_B_SHIFT; +inline constexpr int ImCol32_AShift = IM_COL32_A_SHIFT; +inline constexpr ImU32 ImCol32_AMask = IM_COL32_A_MASK; + +// Size of a static C-style array (= IM_COUNTOF(), but won't compile with pointers) +template constexpr int ImCountOf(const T (&arr)[N]) { return IM_COUNTOF(arr); } + +// Check that version and structures layouts are matching between compiled imgui code and caller (= IMGUI_CHECKVERSION()) +namespace ImGui +{ + inline bool CheckVersion() { return IMGUI_CHECKVERSION(); } +} +#endif // #ifdef IMGUI_HAS_CONSTEXPR + //----------------------------------------------------------------------------- // [SECTION] Obsolete functions and types // (Will be removed! Read 'API BREAKING CHANGES' section in imgui.cpp for details) diff --git a/imgui_macros.h b/imgui_macros.h index 5750fb466..e8189f0d9 100644 --- a/imgui_macros.h +++ b/imgui_macros.h @@ -36,6 +36,12 @@ #define IMGUI_IMPL_API IMGUI_API #endif +// Constexpr alternatives to some of the macros below (e.g. ImCol32() for IM_COL32()) are declared in imgui.h ('Constexpr alternatives to macros' section) when compiling as C++17 or later. +// (C++17 is required for 'inline' variables) +#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L +#define IMGUI_HAS_CONSTEXPR +#endif + // Helper Macros // (note: compiling with NDEBUG will usually strip out assert() to nothing, which is NOT recommended because we use asserts to notify of programmer mistakes.) #ifndef IM_ASSERT