Стандарт C++ 11 (1119564), страница 15
Текст из файла (страница 15)
— end note ]5A program is ill-formed if the definition of any object gives the object an incomplete type (3.9).3.2One definition rule[basic.def.odr]1No translation unit shall contain more than one definition of any variable, function, class type, enumerationtype, or template.2An expression is potentially evaluated unless it is an unevaluated operand (Clause 5) or a subexpressionthereof.
A variable whose name appears as a potentially-evaluated expression is odr-used unless it is anobject that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalueconversion (4.1) is immediately applied. 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 (9.3.1)).A virtual member function is odr-used if it is not pure.
A non-overloaded function whose name appearsas a potentially-evaluated expression or a member of a set of candidate functions, if selected by overloadresolution when referred to from a potentially-evaluated expression, is odr-used, unless it is a pure virtualfunction and its name is not explicitly qualified. [ Note: This covers calls to named functions (5.2.2), operatoroverloading (Clause 13), user-defined conversions (12.3.2), allocation function for placement new (5.3.4), aswell as non-default initialization (8.5). A copy constructor or move constructor is odr-used even if the callis actually elided by the implementation. — end note ] An allocation or deallocation function for a class isodr-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 odr-used by a delete expression appearing in a potentially-evaluatedexpression as specified in 5.3.5 and 12.5.
A non-placement allocation or deallocation function for a class isodr-used by the definition of a constructor of that class. A non-placement deallocation function for a class isodr-used by the definition of the destructor of that class, or by being selected by the lookup at the point ofdefinition of a virtual destructor (12.4).26 A copy-assignment function for a class is odr-used by an implicitlydefined copy-assignment function for another class as specified in 12.8.
A move-assignment function for aclass is odr-used by an implicitly-defined move-assignment function for another class as specified in 12.8. Adefault constructor for a class is odr-used by default initialization or value initialization as specified in 8.5.A constructor for a class is odr-used as specified in 8.5. A destructor for a class is odr-used as specifiedin 12.4.3Every program shall contain exactly one definition of every non-inline function or variable that is odr-usedin that program; no diagnostic required. The definition can appear explicitly in the program, it can be found26) An implementation is not required to call allocation and deallocation functions from constructors or destructors; however,this is a permissible implementation technique.§ 3.236© ISO/IEC 2011 – All rights reservedISO/IEC 14882:2011(E)in the standard or a user-defined library, or (when appropriate) it is implicitly defined (see 12.1, 12.4 and12.8).
An inline function shall be defined in every translation unit in which it is odr-used.4Exactly one definition of a class is required in a translation unit if the class is used in a way that requires theclass type to be complete. [ Example: the following complete translation unit is well-formed, even though itnever defines X:struct X;struct X* x1;X* x2;// declare X as a struct type// use X in pointer formation// use X in pointer formation— end example ] [ Note: The rules for declarations and expressions describe in which contexts complete classtypes are required. A class type T must be complete if:— an object of type T is defined (3.1), or— a non-static class data member of type T is declared (9.2), or— T is used as the object type or array element type in a new-expression (5.3.4), or— an lvalue-to-rvalue conversion is applied to a glvalue 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 thetype pointer to T or reference to T using an implicit conversion (Clause 4), a dynamic_cast (5.2.7) ora 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— a class with a base class of type T is defined (Clause 10), or— an lvalue of type T is assigned to (5.17), or— the type T is the subject of an alignof expression (5.3.6), or— an exception-declaration has type T, reference to T, or pointer to T (15.3).— end note ]5There can be more than one definition of a class type (Clause 9), enumeration type (7.2), inline function withexternal linkage (7.1.2), class template (Clause 14), non-static function template (14.5.6), static data memberof a class template (14.5.1.3), member function of a class template (14.5.1.1), or template specialization forwhich some template parameters are not specified (14.7, 14.5.5) in a program provided that each definitionappears in a different translation unit, and provided the definitions satisfy the following requirements.
Givensuch 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 definedwithin the definition of D, or shall refer to the same entity, after overload resolution (13.3) and aftermatching of partial template specialization (14.8.3), except that a name can refer to a const objectwith internal or no linkage if the object has the same literal type in all definitions of D, and the objectis initialized with a constant expression (5.19), and the value (but not the address) of the object isused, and the object has the same value in all definitions of D; and§ 3.2© ISO/IEC 2011 – All rights reserved37ISO/IEC 14882:2011(E)— 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 asif its token sequence were present in the definition of D; that is, the default argument is subject tothe three requirements described above (and, if the default argument has sub-expressions with defaultarguments, this requirement applies recursively).27— if D is a class with an implicitly-declared constructor (12.1), it is as if the constructor was implicitlydefined in every translation unit where it is odr-used, and the implicit definition in every translationunit 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;//translation unit 2:struct X {X(int);X(int, int);};X::X(int = 0, int = 0) { }class D: public X { };// X(int) called by D()// 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 shallapply both to names from the template’s enclosing scope used in the template definition (14.6.3), and also todependent 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 satisfythese requirements, then the behavior is undefined.3.3Scope3.3.11Declarative regions and scopes[basic.scope][basic.scope.declarative]Every name is introduced in some portion of program text called a declarative region, which is the largest partof the program in which that name is valid, that is, in which that name may be used as an unqualified nameto refer to the same entity.