ArticleslgStudy

science

One Definition Rule

One Definition Rule is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand One Definition Rule rather than just read about it. In short: The One Definition Rule (ODR) is an important rule of the C++ programming language that prescribes that classes/structs and non-inline functions cannot have more than one definition in the entire program and templates and types cannot have more than one definition by translation unit. It is defined in the ISO C++ Standard (ISO/IEC 14882) 2003, at section 3.2.

Key takeaways

  • One Definition Rule belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect One Definition Rule to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of One Definition Rule from memory before moving on to harder problems.

Reference excerpt

The One Definition Rule (ODR) is an important rule of the C++ programming language that prescribes that classes/structs and non-inline functions cannot have more than one definition in the entire program and templates and types cannot have more than one definition by translation unit. It is defined in the ISO C++ Standard (ISO/IEC 14882) 2003, at section 3.2. Some other programming languages have similar but differently defined rules towards the same objective.

Summary In short, the ODR states that:

In any translation unit, a template, type, function, or object can have no more than one definition. Some of these can have any number of declarations. A definition provides an instance. In the entire program, an object or non-inline function cannot have more than one definition; if an object or function is used, it must have exactly one definition. You can declare an object or function that is never used, in which case you don't have to provide a definition. In no event can there be more than one definition. Some things, like types, templates, and extern inline functions, can be defined in more than one translation unit. For a given entity, each definition must have the same sequence of tokens. Non-extern objects and functions in different translation units are different entities, even if their names and types are the same. Some violations of the ODR must be diagnosed by the compiler. Other violations, particularly those that span translation units, are not required to be diagnosed.

Examples In general, a translation unit shall contain no more than one definition of any class type. In this example, two definitions of the class type MyClass occur in the same translation unit. This typically occurs if a header file is included twice by the same source file without appropriate header guards.

In the following, forming a pointer to MyStruct or defining a function taking a reference to MyStruct are examples of legal constructs, because they do not require the type of MyStruct to be complete. Therefore, a definition is not required. Defining an object of type MyStruct, a function taking an argument of type MyStruct, or using MyStruct in a sizeof expression are examples of contexts where S must be complete, and therefore require a definition.

More than one definition In certain cases, there can be more than one definition of a type or a template. A program consisting of multiple header files and source files will typically have more than one definition of a type, but not more than one definition per translation unit. If a program contains more than one definition of a type, then each definition must be equivalent.

Definitions of static const data members In pre-standard C++, all static data members required a definition outside of their class. However, during the C++ standardization process it was decided to lift this requirement for static const integral members. The intent was to allow uses such as:

without a namespace scope definition for N. Nevertheless, the wording of the 1998 C++ standard still required a definition if the member was used in the program. This included the member appearing anywhere except as the operand to sizeof or typeid, effectively making the above ill-formed. This was identified as a defect, and the wording was adjusted to allow such a member to appear anywhere a constant expression is required, without requiring an out-of-class definition. This includes array bounds, case expressions, static member initializers, and nontype template arguments.

However, using a static const integral member anywhere except where an integral constant-expression is required, requires a definition:

This requirement was relaxed in a later standard, C++11.

Example showing unexpected side effects We need 4 files: "Base.cppm", "Dummy1.cppm", "Dummy2.cppm", "Main.cpp". Base.cppm:

Dummy1.cppm

Dummy2.cppm:

Main.cpp:

When executed the expected output is:

odr ONE dummy: Hello odr TWO dummy: World

But the likely output is:

odr ONE dummy: Hello odr ONE dummy: Hello

The problem is, that the C++ linker has to figure out how to build the virtual method table for the (two different) Dummy classes, and that only works if the class names are different.

See also Include directive Include guard

References

Worked examples

Example 1 — a first encounter with One Definition Rule

Start with the simplest possible case. Write down what One Definition Rule claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to One Definition Rule before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about One Definition Rule ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of One Definition Rule

In research
One Definition Rule appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses One Definition Rule in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
One Definition Rule is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, so understanding it makes those chapters shorter.
In everyday life
Look for One Definition Rule outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “One Definition Rule” →

How to study One Definition Rule in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what One Definition Rule means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain One Definition Rule out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is One Definition Rule in simple terms?

The One Definition Rule (ODR) is an important rule of the C++ programming language that prescribes that classes/structs and non-inline functions cannot have more than one definition in the entire program and templates and types cannot have more than one definition by translation unit. It is defined…

Why does One Definition Rule matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study One Definition Rule?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on One Definition Rule.

Tags

  • C++

Keep exploring