How to Use ImVec2 Math Operators with `IMGUI_DEFINE_MATH_OPERATORS` in Dear ImGui
Define IMGUI_DEFINE_MATH_OPERATORS before including imgui.h to enable arithmetic operators (+, -, *, /) for ImVec2 and ImVec4 vectors.
Dear ImGui (ocornut/imgui) provides lightweight ImVec2 and ImVec4 structures for 2D and 4D vector data. By default, the library only exposes data members (x, y) and minimal utility functions to maintain portability and fast compile times. To write expressive vector arithmetic like pos = a + b * scale; instead of verbose member-wise operations, you must explicitly enable the optional ImVec2 math operators through a compile-time macro.
Enabling ImVec2 Math Operators
The arithmetic operators are implemented as inline functions guarded by the IMGUI_DEFINE_MATH_OPERATORS macro in imgui.h (around lines 2918–2930). Enabling them requires defining this macro in exactly one of two locations.
Global Configuration via imconfig.h
For project-wide availability, add the definition to your imconfig.h file, which Dear ImGui automatically includes:
// imconfig.h
#define IMGUI_DEFINE_MATH_OPERATORS
This approach ensures every source file that includes imgui.h has access to the operators without additional boilerplate.
Local Configuration in Source Files
To enable operators only in specific translation units, define the macro immediately before the ImGui header inclusion:
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
void example() {
ImVec2 a(10.0f, 20.0f);
ImVec2 b(5.0f, 5.0f);
float scale = 2.0f;
// Now available: +, -, *, /, unary minus, and compound assignments
ImVec2 c = a + b * scale; // (20, 30)
ImVec2 d = (a - b) / scale; // (2.5, 7.5)
}
Important: The macro must be defined before the first inclusion of imgui.h. Defining it after inclusion has no effect, as the operator definitions are conditionally compiled at parse time.
Using Operators with Custom Vector Types
You can leverage the enabled ImVec2 math operators with your own vector classes by providing implicit conversion constructors. This allows seamless interoperability without requiring additional operator overloads for your custom type.
Implementation Pattern
Provide a constructor that accepts ImVec2 and a conversion operator to ImVec2:
struct MyVec2 {
float x, y;
MyVec2() = default;
MyVec2(float _x, float _y) : x(_x), y(_y) {}
// Implicit conversion from ImVec2
MyVec2(const ImVec2& v) : x(v.x), y(v.y) {}
// Implicit conversion to ImVec2
operator ImVec2() const { return ImVec2(x, y); }
};
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
void demo() {
MyVec2 position(1.0f, 2.0f);
MyVec2 velocity(3.0f, 4.0f);
ImVec2 offset(5.0f, 5.0f);
// Works thanks to implicit conversions:
MyVec2 result = position + velocity * 0.5f + offset; // (7.5, 9.0)
}
Because MyVec2 converts to and from ImVec2, the compiler can utilize the operators defined in imgui_internal.h (referenced from imgui.h) to perform arithmetic on your custom type. These operators are purely syntactic sugar forwarding to internal inline functions, incurring zero runtime overhead compared to manual member-wise operations.
Note: If you encounter ambiguous overload resolution with complex expressions, use explicit casting (ImVec2(myVec)) rather than relying on implicit conversions, or provide explicit operator overloads for your specific type.
Where the Implementation Lives
According to the ocornut/imgui source code, the operator implementations are structured as follows:
- Declaration guard: Located in
imgui.h(~line 2918) under#ifdef IMGUI_DEFINE_MATH_OPERATORS - Inline definitions: Fwd to utilities in
imgui_internal.h(~line 115), which provides the actualoperator+,operator-,operator*, andoperator/implementations - Configuration template:
imconfig.hserves as the recommended location for project-wide macro definitions
Summary
- Define
IMGUI_DEFINE_MATH_OPERATORSbefore includingimgui.hto enable arithmetic operators forImVec2andImVec4. - Place the definition in
imconfig.hfor global availability, or at the top of specific.cppfiles for local use. - Integrate custom types by providing implicit conversion constructors to/from
ImVec2, allowing the built-in operators to work seamlessly. - Operators are zero-cost abstractions that compile to the same instructions as manual member-wise math.
Frequently Asked Questions
Where should I define IMGUI_DEFINE_MATH_OPERATORS?
Define it in your imconfig.h file for project-wide availability, or at the top of any source file before #include "imgui.h" if you only need the operators in specific translation units. Never define it in a header that might be included after imgui.h, as the macro must be active when the header is first parsed.
Do the math operators add performance overhead?
No. The operators are inline functions defined in imgui_internal.h that compile to identical machine code as manual operations like ImVec2(a.x + b.x, a.y + b.y). They provide syntactic convenience without runtime cost.
Can I use these operators with ImVec4 as well?
Yes. Defining IMGUI_DEFINE_MATH_OPERATORS enables operators for both ImVec2 and ImVec4 structures simultaneously, including addition, subtraction, multiplication, division, and unary negation for both types.
Why aren't these operators enabled by default?
Dear ImGui prioritizes minimal compile times and avoids potential symbol conflicts with user-defined math libraries. By guarding the operators behind IMGUI_DEFINE_MATH_OPERATORS, the library remains portable while allowing opt-in convenience for developers who need expressive vector arithmetic.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →