Definitions and ODR
Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following:
extern const int a; // declares, but doesn't define a extern const int b = 1; // defines b
int f(int); // declares, but doesn't define f
int f(int x); // declares, but doesn't define f and x int f(int x) { // defines f and x return x+a; }
struct S { // defines S int n; // defines S::n static int i; // declares, but doesn't define S::i }; int S::i; // defines S::i
struct S; // declares, but doesn't define S class Y f(class T p); // declares, but doesn't define Y and T (and also f and p)
typedef S S2; // declares, but doesn't define S2 (S may be incomplete)
- (C++11) alias-declaration
using S2 = S; // declares, but doesn't define S2 (S may be incomplete)
- (C++11) Opaque declaration of an enumeration.
enum Color : int; // declares, but doesn't define Color
using N::d; // declares, but doesn't define d
- using-directive (does not define any entities)
- (C++11) static_assert declaration (does not define any entities)
- (C++11) Attribute declaration (does not define any entities)
- Empty declaration (does not define any entities)
Although asm declaration does not define any entities as well, it is a definition.
Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor.
If the definition of any object results in an object of incomplete type, the program is ill-formed.
[edit] One Definition Rule
Only one definition of any variable, function, class type, enumeration type, or template is allowed in any one translation unit.
One and only one definition of every non-inline function or variable that is odr-used (see below) is required to appear in the entire program (including any standard and user-defined libraries). The compiler is not required to diagnose this violation, but the behavior of the program that violates it is undefined.
For an inline function, a definition is required in every translation unit where it is odr-used
One and only one definition of a class is required to appear in any translation unit where the class is used in a way that requires it to be complete.
There can be more than one definition in a program, as long as each definition appears in a different translation unit, of each of the following: class type, enumeration type, inline function with external linkage, class template, non-static function template, static data member of a class template, member function of a class template, partial template specialization, as long as all of the following is true:
- each definition consists of the same sequence of tokens (typically, appears in the same header file)
- name lookup from within each definition finds the same entities (after overload-resolution), except that constants with internal or no linkage may refer to different objects as long as they are not ODR-used and have the same values in every definition.
- overloaded operators, including conversion, allocation, and deallocation functions refer to the same function from each definition (unless referring to one defined within the definition)
- the language linkage is the same (e.g. the include file isn't inside an extern "C" block)
- the three rules above apply to every default argument used in each definition
- if the definition is for a class with an implicitly-declared constructor, every translation unit where it is odr-used must call the same constructor for the base and members
- if the definition is for a template, then all these requirements apply to both names at the point of definition and dependent names at the point of instantiation
If all these requirements are satisfied, the program behaves as if there is only one definition in the entire program. Otherwise, the behavior is undefined.
[edit] ODR-use
- a variable is odr-used if its name appears as a potentially-evaluated expression, except if all of the following is true:
-
- applying lvalue-to-rvalue conversion to the exression yields a constant expression that doesn't invoke non-trivial functions
- the expression is either discarded-value expression or an lvalue-to-rvalue conversion
- this is odr-used if it appears as a potentially-evaluated expression (including the implicit this in a non-static member function call expression)
- virtual member function is odr-used if it is not a pure virtual member function
A function whose name appears as a potentially-evaluated expression is odr-used if it is the unique lookup result or the selected member of a set of overloaded functions
This section is incomplete Reason: work in progress: rest of 3.2 and templates, micro-examples as needed |