What is the One Definition Rule (ODR)?
Short answer
The ODR states that every entity (variable, function, class, template) must have exactly one definition across the whole program (with specific exceptions: inline functions/variables and templates may be defined identically in multiple translation units).
Advertisement
AModel answer
The ODR states that every entity (variable, function, class, template) must have exactly one definition across the whole program (with specific exceptions: inline functions/variables and templates may be defined identically in multiple translation units). Violating the ODR—e.g., two different definitions of the same function—is undefined behavior, often a link error or subtle bug.
Real-world example
Defining a non-inline function in a header included by two files causes an ODR-violation multiple-definition link error.
Likely follow-ups
- What are the ODR exceptions? | What happens if you violate it?
Related topics