• Definitions


    Definitions and ODR

    Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following:
    下面都是declaration而不是definition

    extern const int a; // declares, but doesn't define a
    extern const int b = 1; // defines b
    
    • A function declaration without a function body
    int f(int);			// declares, but doesn't define f
    
    • A parameter declaration in a function declaration that isn't a definition
    int f(int x); // declares, but doesn't define f and x
    int f(int x) { // defines f and x
         return x+a;
    }
    
    • Declaration of a static data member inside a class definition
    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
    
    • Declaration of a class name (by forward declaration or by the use of the elaborated type specifier in another declaration)
    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)
    
    • Declaration of a template parameter
    template<typename T>	// declares, but doesn's define T
    
    • An explicit specialization whose declaration is not a definition.
    template<> struct A<int>; // declares, but doesn't define A<int>
    
    • A typedef declaration
    typedef S S2; // declares, but doesn't define S2 (S may be incomplete)
    
    • An alias-declaration(since C++11)
    using S2 = S; // declares, but doesn't define S2 (S may be incomplete)
    
    • An opaque declaration of an enumeration(since C++11)
    enum Color : int; // declares, but doesn't define Color
    
    • A using-declaration
    using N::d;	// declares, but doesn't define d
    
    • A using-directive (does not define any entities)(since C++11)
    • A static_assert declaration (does not define any entities)
    • An attribute declaration (does not define any entities)
    • An explicit instantiation declaration (an "extern template")
      extern template f<int, char>; // declares, but doesn't define f<int, char>
    • An empty declaration (does not define any entities)

    Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor.

    One Definition Rule=ODR

    Only one definition of any variable, function, class type, enumeration type, or template is allowed in any one translation unit (some of these may have multiple declarations, but only one definition is allowed).

    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.

    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 be the same. 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.

  • 相关阅读:
    WebService之Axis2 (3):使用services.xml文件发布WebService
    WebService之Axis2(2):复合类型数据的传递
    WebService之Axis2(1):用POJO实现0配置的WebService
    com.mchange.v2.c3p0.impl.NewPooledConnection@be1839d closed by a client的正确解答
    拿到新服务器时应做的标准测试
    rabbitmq heartbeat missing with heartbeat = N seconds原因总结
    从技术专家到管理者的思路转变(V1)
    netty集成ssl完整参考指南(含完整源码)
    技术晋升的评定与博弈(转)
    Eureka-zookeeper的服务发现替代方案
  • 原文地址:https://www.cnblogs.com/Wojoin/p/5168066.html
Copyright © 2020-2023  润新知