C++ Reference ยท Types and Language Concepts
The One Definition Rule (ODR)
Concept page on the C++ One Definition Rule: what may be defined once or many times, inline functions and variables, headers, linkage and ODR violations.
The One Definition Rule (ODR) says how many times an entity may be defined. A declaration introduces a name (int f(int);); a definition also provides the body or storage (int f(int x) { return x; }). Roughly, a non-inline function or variable may be defined exactly once in the whole program, while classes, templates, inline functions and inline variables may be defined in several translation units provided every definition is token-for-token the same and means the same thing.
This is the reason headers contain declarations and class definitions but not ordinary function bodies. A violation is usually ill-formed, no diagnostic required: the linker may report a duplicate symbol, or it may silently pick one definition, and the program misbehaves.
// header.h int counter; // BAD: a definition in every file that includes it inline int counter2 = 0; // OK (C++17): one entity shared by all files
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The rules in practice
What counts as a definition
A definition is any declaration that is not just a declaration: a function with a body, a variable without extern, a class with its member list, an enumeration with enumerators, a template with its body. Member functions defined inside the class body are implicitly inline, and so are static constexpr data members since C++17.
Allowed more than once
The following may appear in several translation units, as long as the definitions are identical and consist of the same tokens, with names resolving to the same entities:
- class and enum definitions
inlinefunctions and variables (and everything implicitly inline: member functions defined in the class,constexprfunctions,constevalfunctions)- templates (both class and function templates) and their member definitions
- namespace-scope variables declared
inline(C++17); plainconstandconstexprvariables at namespace scope have internal linkage, so each file simply has its own copy
Exactly once
- ordinary (non-inline) functions
- ordinary (non-inline) variables with external linkage
- static data members that are not
inlineorconstexpr: declare them in the class, define them in one.cpp
Linkage tools
| Tool | Effect |
|---|---|
static at namespace scope |
Internal linkage: the name is private to this file. Prefer an unnamed namespace in new code. |
| Unnamed namespace | Same, and also works for types. |
inline |
Allows multiple identical definitions; each translation unit that uses it needs the definition visible. |
extern |
Declares a variable that is defined elsewhere (extern int counter;). |
Usage notes
- Common violation: two files define a class with the same name but different members (even a changed
#ifdefcan do this). The linker sees no conflict, yet calls through one definition use another's layout, causing crashes. Wrap file-local classes in an unnamed namespace. - Duplicate-symbol link errors (
multiple definition of 'f') are the friendly ODR violation. They mean a non-inline function or variable is defined in a header. inlineis not about inlining. The compiler decides on its own whether to expand a call. The keyword exists for linkage.- Header guards and
#pragma oncestop one file from including a header twice; they do not stop two files from each defining a function. The ODR is a program-wide rule. - Templates are fine in headers because the standard treats their definitions as inline-like; the same function template instantiated in two files is merged.
- Default arguments and
constexpr/inlinefunction bodies must also be identical in every translation unit. - Modules eliminate many ODR accidents because a module exports one definition (see Modules).