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:

  1. 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.
  2. 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.
  3. 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 <code>MyClass</code> 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.

<syntaxhighlight lang="cpp">

class MyClass {}; // first definition of MyClass

class MyClass {}; // error, second definition of MyClass

</syntaxhighlight>

In the following, forming a pointer to <code>MyStruct</code> or defining a function taking a reference to <code>MyStruct</code> are examples of legal constructs, because they do not require the type of <code>MyStruct</code> to be complete. Therefore, a definition is not required.

Defining an object of type <code>MyStruct</code>, a function taking an argument of type <code>MyStruct</code>, or using <code>MyStruct</code> in a sizeof expression are examples of contexts where S must be complete, and therefore require a definition.

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:

<syntaxhighlight lang="cpp">

struct MyClass {

static const int N = 10;

};

char data[MyClass::N]; // N "used" without out-of-class definition

</syntaxhighlight>

without a namespace scope definition for <code>N</code>.

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.

<syntaxhighlight lang="cpp">

struct MyClass1 {

static const int N = 10;

static const int U = N; // Legal per C++03

};

char data[Class1::N]; // Legal per C++03

template <int>

struct MyClass2;

template <>

struct MyClass2<Class1::N> {}; // Legal per C++03

</syntaxhighlight>

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

<syntaxhighlight lang="cpp">

struct MyClass {

static const int N = 10;

};

int main() {

int i = MyClass::N; // Ill-formed in C++03. Definition of MyClass::N required.

}

</syntaxhighlight>

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