### 3 基本概念 【基本】

#### 3.2 唯一定义原则 【基本.定义.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 either it is the operand of the sizeof operator (5.3.3), or it is the operand of the typeid operator and does not designate an lvalue of polymorphic class type (5.2.8). An object or non-overloaded function is used if its name appears in a potentially-evaluated expression. A virtual member function is used if it is not pure. An overloaded function is used if it is selected by overload resolution where referred to from a potentially-evaluated expression. [Note: this covers calls to named functions (5.2.2), operator overloading (clause 13), user-defined conversions (12.3.2), allocation function for placement new (5.3.4), as well as non-default initialization (8.5). A copy constructor is used even if the call is actually elided by the implementation. ] An allocation or deallocation function for a class is used by a new expression appearing in a potentially-evaluated expression as specified in 5.3.4 and 12.5. A deallocation function for a class is used by a delete expression appearing in a potentially-evaluated expression as specified in 5.3.5 and 12.5. A copy-assignment function for a class is used by an implicitly-defined copy-assignment function for another class as specified in 12.8. A default constructor for a class is used by default initialization as specified in 8.5. A constructor for a class is used as specified in 8.5. A destructor for a class is used as specified in 12.4.

Every program shall contain exactly one definition of every non-inline function or object that is 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 appropriated) it is implicitly defined (see 12.1, 12.4 and 12.8). An inline function shall be defined in every translation unit in which it is 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:

• an object of type T is defined (3.1, 5.3.4), or
• an lvalue-to-rvalue conversion is applied to an lvalue referring to an object of type T (4.1), or
• an expression is converted (either implicitly or explicitly) to type T (clause 4, 5.2.3, 5.2.7, 5.2.9, 5.4), or
• an expression that is not a null pointer constant, and has type other than void *, is converted to the type pointer to T or reference to T using an implicit conversion (clause 4), a dynamic_cast (5.2.7) or a static_cast (5.2.9), or
• a class member access operator is applied to an expression of type T (5.2.5), or
• the typeid operator (5.2.8) or the sizeof operator (5.3.3) is applied to an operand of type T, or
• a function with a return type or argument type of type T is defined (3.1) or called (5.2.2), or
• an lvalue of type T is assigned to (5.17). ]

    struct X;                       // 声明 X 为结构体类型
    struct X* x1;                   // 以指针形式应用 X
    X* x2;                          // 以指针形式应用 X

--例完】【注：描述声明和定义在何中语境中需要完整类型的规则。某类型 T 必须完整，当：

• 类型 T 的一个对象被定义（3.1，5.3.4），或者
• 左值道右值转换被应用到一个引用类型 T 的一个对象（4.1）的左值，或者
• 表达式被（隐式或显式）转换为类型 T（章节 4，5.2.3，5.2.7，5.2.9，5.4），或者
• 不是空指针常量，并且不具有类型 void *，使用隐式转换（章节 4），dynamic_cast（5.2.7）或 static_cast（5.2.9）被转换为类型 T 的指针或 T 的引用，或者
• 类成员访问运算符被应用到具有类型 T 的表达式上（5.2.5），或者
• typeid 运算符（5.2.8）或 sizeof 运算符（5.3.3）被应用到一个类型 T 的操作数，或者
• 返回类型或参数类型为类型 T 的函数被定义（3.1）或被调用（5.2.2），或者
• 类型 T 的左值被赋值（5.17）。】

There can be more than one definition of a class type (clause 9), enumeration type (7.2), inline function with external linkage (7.1.2), class template (clause 14), non-static function template (14.5.5), static data member of a class template (14.5.1.3), member function template (14.5.1.1), or template specialization for which some template parameters are not specified (14.7, 14.5.4) 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 3.4, shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution (13.3) and after matching of partial template specialization (14.8.3), except that a name can refer to a const object with internal or no linkage if the object has the same integral or enumeration type in all definitions of D, and the object is initialized with a constant expression (5.19), and the value (but not the address) of the object is used, and the object has the same value in all definitions of D; 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).25)
• if D is a class with an implicitly-declared constructor (12.1), it is as if the constructor was implicitly defined in every translation unit where it is used, and the implicit definition in every translation unit shall call the same constructor for a base 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 last four requirements from the list above shall apply to names from the template's enclosing scope used in the template definition (14.6.3), and also to dependent names at the point of instantiation (14.6.2). If the definitions of D satisfy all these requirements, then the program shall behave as if there were a single definition of D. If the definitions of D do not satisfy these requirements, then the behavior is undefined.

• 每个 D 的定义应该由相同的标记序列组成；并且
• 每个 D 的定义，根据 3.4 查找的相应名字，在重载解析（13.3）之后并在模板部分特化（14.8.3）匹配之后，应当指代 D 的定义中定义的实体，或者指代相同实体，除非名字指代具有内部连接性或无连接性的 const 对象，且对象在所有 D 定义中具有相同的整数或枚举类型，同时对象由常量表达式（5.19）初始化，并且对象被使用值（而不是地址），且其值在所有 D 的定义中具有相同的值；并且
• 每个 D 的定义中，重载运算符，类型转换函数的隐式调用，构造函数，new 运算符函数和 delete 运算符函数应该指代相同函数，或在 D 中定义的函数；并且
• 每个 D 的定义中的（隐式或显式）函数调用所使用的默认参数，其标记序列被认为同样在 D 的定义中出现；也就是说，默认参数须符合上述三条要求（并且，如果默认参数具有带有默认参数的子表达式，这些要求被递归应用）。25)
• 如果 D 是带有隐式声明构造函数（12.1）的类，则每个翻译单元都应当隐式定义了这个构造函数，且每个翻译单元的隐式定义应当调用应当调用 D 基类成员的相同构造函数。【例：

    // 翻译单元 1:
    struct X {
        X(int);
        X(int, int);
    };
    X::X(int = 0) { }
    class D: public X { };
    D d2;                           // X(int) D() 调用

    // 翻译单元 2:
    struct X {
        X(int);
        X(int, int);
    };
    X::X(int = 0, int = 0) { }
    class D: public X { };          // X(int, int) D() 调用；
                                    // D() 的隐式定义
                                    // 违反 ODR

--例完】如果 D 是模板，并在多于一个翻译单元中定义，则以上后四个要求应该被应用到该模板定义（14.6.3）包含的范围中应用名字上，并同样应用到由实例化点（14.6.2）决定的名字上。如果 D 的所有定义满足以上条件，则程序应当表现为仅有一个 D 的定义。如果 D 的任何定义不满足以上条件，则其行为是未定义的。

25) 8.3.6 describes how default argument names are looked up.

25) 8.3.6 节描述怎样进行默认参数查找

PREV [basic.def] | NEXT [basic.scope] 上一页 【基本.定义】 | 下一页 【基本.作用域】

0 0