Back to results for “Templates”
C++ › Compilation & Linking

What is the One Definition Rule (ODR)?

Intermediate

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.

Common follow-ups: What are the ODR exceptions? | What happens if you violate it?