3 Basic concepts [basic]

3.2 One definition rule [basic.def.odr]

No translation unit shall contain more than one definition of any variable, function, class type, enumeration type, or template.

An expression is potentially evaluated unless it is an unevaluated operand (Clause [expr]) or a subexpression thereof. The set of potential results of an expression e is defined as follows:

  • If e is an id-expression ([expr.prim.general]), the set contains only e.

  • If e is a class member access expression ([expr.ref]), the set contains the potential results of the object expression.

  • If e is a pointer-to-member expression ([expr.mptr.oper]) whose second operand is a constant expression, the set contains the potential results of the object expression.

  • If e has the form (e1), the set contains the potential results of e1.

  • If e is a glvalue conditional expression ([expr.cond]), the set is the union of the sets of potential results of the second and third operands.

  • If e is a comma expression ([expr.comma]), the set contains the potential results of the right operand.

  • Otherwise, the set is empty.

Note: This set is a (possibly-empty) set of id-expressions, each of which is either e or a subexpression of e. [ Example: In the following example, the set of potential results of the initializer of n contains the first S::x subexpression, but not the second S::x subexpression.

struct S { static const int x = 0; };
const int &f(const int &r);
int n = b ? (1, S::x)  // S::x is not odr-used here
          : f(S::x);   // S::x is odr-used here, so
                       // a definition is required

 — end example ]  — end note ]

A variable x whose name appears as a potentially-evaluated expression ex is odr-used by ex unless applying the lvalue-to-rvalue conversion ([conv.lval]) to x yields a constant expression ([expr.const]) that does not invoke any non-trivial functions and, if x is an object, ex is an element of the set of potential results of an expression e, where either the lvalue-to-rvalue conversion ([conv.lval]) is applied to e, or e is a discarded-value expression (Clause [expr]). this is odr-used if it appears as a potentially-evaluated expression (including as the result of the implicit transformation in the body of a non-static member function ([class.mfct.non-static])). A virtual member function is odr-used if it is not pure. 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 ([basic.lookup], [over.match], [over.over]), unless it is a pure virtual function and its name is not explicitly qualified. [ Note: This covers calls to named functions ([expr.call]), operator overloading (Clause [over]), user-defined conversions ([class.conv.fct]), allocation function for placement new ([expr.new]), as well as non-default initialization ([dcl.init]). A constructor selected to copy or move an object of class type is odr-used even if the call is actually elided by the implementation ([class.copy]).  — end note ] An allocation or deallocation function for a class is odr-used by a new expression appearing in a potentially-evaluated expression as specified in [expr.new] and [class.free]. A deallocation function for a class is odr-used by a delete expression appearing in a potentially-evaluated expression as specified in [expr.delete] and [class.free]. A non-placement allocation or deallocation function for a class is odr-used by the definition of a constructor of that class. A non-placement deallocation function for a class is odr-used by the definition of the destructor of that class, or by being selected by the lookup at the point of definition of a virtual destructor ([class.dtor]).26 An assignment operator function in a class is odr-used by an implicitly-defined copy-assignment or move-assignment function for another class as specified in [class.copy]. A default constructor for a class is odr-used by default initialization or value initialization as specified in [dcl.init]. A constructor for a class is odr-used as specified in [dcl.init]. A destructor for a class is odr-used if it is potentially invoked ([class.dtor]).

Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. The definition can appear explicitly in the program, it can be found in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see [class.ctor], [class.dtor] and [class.copy]). An inline function shall be defined in every translation unit in which it is odr-used.

Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class type to be complete. [ Example: the following complete translation unit is well-formed, even though it never defines X:

struct X;                       // declare X as a struct type
struct X* x1;                   // use X in pointer formation
X* x2;                          // use X in pointer formation

 — end example ] [ Note: The rules for declarations and expressions describe in which contexts complete class types are required. A class type T must be complete if:

 — end note ]

There can be more than one definition of a class type (Clause [class]), enumeration type ([dcl.enum]), inline function with external linkage ([dcl.fct.spec]), class template (Clause [temp]), non-static function template ([temp.fct]), static data member of a class template ([temp.static]), member function of a class template ([temp.mem.func]), or template specialization for which some template parameters are not specified ([temp.spec], [temp.class.spec]) in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then

  • each definition of D shall consist of the same sequence of tokens; and

  • in each definition of D, corresponding names, looked up according to [basic.lookup], shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution ([over.match]) and after matching of partial template specialization ([temp.over]), except that a name can refer to a non-volatile const object with internal or no linkage if the object has the same literal type in all definitions of D, and the object is initialized with a constant expression ([expr.const]), and the object is not odr-used, and the object has the same value in all definitions of D; and

  • in each definition of D, corresponding entities shall have the same language linkage; and

  • in each definition of D, the overloaded operators referred to, the implicit calls to conversion functions, constructors, operator new functions and operator delete functions, shall refer to the same function, or to a function defined within the definition of D; and

  • in each definition of D, a default argument used by an (implicit or explicit) function call is treated as if its token sequence were present in the definition of D; that is, the default argument is subject to the three requirements described above (and, if the default argument has sub-expressions with default arguments, this requirement applies recursively).27

  • if D is a class with an implicitly-declared constructor ([class.ctor]), it is as if the constructor was implicitly defined in every translation unit where it is odr-used, and the implicit definition in every translation unit shall call the same constructor for a base class or a class member of D. [ Example:

    //translation unit 1:
    struct X {
      X(int);
      X(int, int);
    };
    X::X(int = 0) { }
    class D: public X { };
    D d2;                           // X(int) called by D()
    
    //translation unit 2:
    struct X {
      X(int);
      X(int, int);
    };
    X::X(int = 0, int = 0) { }
    class D: public X { };          // X(int, int) called by D();
                                    // D()'s implicit definition
                                    // violates the ODR
    

     — end example ]

If D is a template and is defined in more than one translation unit, then the preceding requirements shall apply both to names from the template's enclosing scope used in the template definition ([temp.nondep]), and also to dependent names at the point of instantiation ([temp.dep]). If the definitions of D satisfy all these requirements, then the behavior is as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

An implementation is not required to call allocation and deallocation functions from constructors or destructors; however, this is a permissible implementation technique.

[dcl.fct.default] describes how default argument names are looked up.