diff --git a/imgui.h b/imgui.h index a336ccd06..99a736c40 100644 --- a/imgui.h +++ b/imgui.h @@ -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) @@ -2150,7 +2150,7 @@ struct ImGuiTableColumnSortSpecs }; //----------------------------------------------------------------------------- -// [SECTION] Helpers: Debug log, memory allocations macros, ImVector<> +// [SECTION] Helpers: Debug log, memory allocations macros, ImVector<>, Unreachable //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -2255,6 +2255,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 , 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 with stdlib >= 23. +//----------------------------------------------------------------------------- +namespace ImGui +{ +//#define STD_UNREACHABLE_IS_AVAILABLE +#ifdef STD_UNREACHABLE_IS_AVAILABLE + #include + 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 //-----------------------------------------------------------------------------