📅 2010-Oct-26 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cpp, odr ⬩ 📚 Archive
Every C++ programmer is aware of the One Definition Rule (ODR), but in a vague sense: “Only one definition of something is allowed in something.” I was surprised to learn that the ODR stumps not just us mortals, but the architects of C++ itself. More than a decade after the birth of C++, in a 1995 C++ Standards meeting it took the architects 2 entire days to put down this rule to paper! 😊
Here is a simple breakdown of the ODR:
In a program, there can be only one definition of:
In a translation unit (a .cpp file), there can be only one definition of:
Alternatively, we can flip around the definition of the rule and try to understand it from the point-of-view of the entities:
Class: There can be only one definition of a class in a translation unit.
(Non-inline) Function or (global) Variable: There can be only one definition of such a function or variable in the entire program.
Standard for C++ Programming Language: The ODR section is arcane and runs to 3 pages!
Section 9.2.3 in The C++ Programming Language by Stroustrup: Surprisingly, the C++ creator himself only describes the ODR with relation to classes, clearly skipping (or assuming) it for functions and variables!
Articles Diary of an Observer by Francis Glassborow and The Casting Vote by Sean A. Corfield in Issue 9 (August 1995) of Overload magazine: C++ architects describe in entertaining detail how nailing the ODR definition took them 2 full days at the C++ standards meeting.