In C++, what does the one definition rule (ODR) state about how many definitions of an entity such as a function or class may exist in a program?

Difficulty: Hard

Correct Answer: Each function, variable, class, or template must have exactly one definition in the entire program, with all declarations referring to the same single definition.

Explanation:


Introduction / Context:
The one definition rule (ODR) is a fundamental rule in C++ that controls how many definitions of an entity such as a function, variable, class, or template may exist in a program. Violating the ODR can lead to undefined behaviour and extremely subtle bugs. This question checks conceptual understanding of the rule, which is essential for correct use of header files and separate compilation.


Given Data / Assumptions:

  • A C++ program may consist of multiple translation units (source files and their included headers).
  • Entities like functions, globals, and classes can be declared in more than one place.
  • However, the language imposes strict limits on how many actual definitions may exist.


Concept / Approach:
The ODR states, in simplified form, that for each variable, function, class type, enumeration type, or template, there must be exactly one definition in the entire program, with a few carefully specified exceptions for inline and template definitions that must still be identical across translation units. Multiple declarations are allowed as long as they all refer to the same single definition. If different definitions with the same name appear in different translation units, the behaviour of the program is undefined.


Step-by-Step Solution:
Step 1: Recognize that you may place a function declaration such as int add(int, int); in a header and include it in many source files. Step 2: Provide one and only one function definition, for example in add.cpp: int add(int a, int b) { return a + b; }. Step 3: The linker then combines all translation units, expecting that all uses of add refer to this single definition. Step 4: If you accidentally create two different definitions of add in different source files, the ODR is violated and the resulting program has undefined behaviour. Step 5: The same rule applies to classes and templates, which must have consistent, single definitions across the program.


Verification / Alternative check:
In practice, many link time errors such as "multiple definition" or "duplicate symbol" are direct consequences of ODR violations. For templates and inline functions, compilers allow multiple identical definitions in different translation units, but they must be exactly the same. Changing a template definition in one translation unit and forgetting to recompile others can lead to subtle mismatches, again violating the ODR and causing runtime anomalies.


Why Other Options Are Wrong:
Option B is incorrect because overloading relies on different parameter lists, not on having multiple definitions of the exact same function signature. Option C is wrong since header files typically contain declarations or inline definitions, not repeated non inline definitions in every translation unit. Option D is incorrect because namespaces allow disambiguation of names, but they do not permit multiple conflicting definitions of the same entity in the same program.


Common Pitfalls:
A frequent mistake is defining non inline functions or global variables in header files that are included in many source files, leading to multiple definitions at link time. Another pitfall is modifying a header that contains a template or inline function and then failing to recompile all translation units that include it, which may result in inconsistent binary definitions. Using include guards, placing definitions in single implementation files, and understanding inline semantics help avoid ODR violations.


Final Answer:
The ODR states that each function, variable, class, or template must have exactly one definition in the entire program, with all declarations referring to the same single definition.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion