zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    Java第一次作业——Java语言基础
    P田日志<1>
    没解决的问题-git连接失败
    解决每次centos7执行java --version git --version等命令时都要重新source /etc/profile后才能执行,否则找不到命令-转载
    【Linux】VMware虚拟机中如何配置静态IP-转载
    Parallels Desktop 16 破解版 出现网络初始化失败和不能连接USB设备解决方法-转载
    Ubuntu安装git时出错,git-compat-util.h:280:25: fatal error: openssl/ssl.h: 没有那个文件或目录-转载
    遇到的问题29
    解决Idea 导入maven工程编译 Error running 'tms [clean]': Cannot run program "C:Program FilesJavajdk --转载
    jenkins执行shell命令时,提示“Command not found”处理方法-需要在jenkins调用shell脚本的最前面加一行脚本, #!/bin/bash -ilex-------转载
  • 原文地址:https://www.cnblogs.com/Wojoin/p/5168066.html
Copyright © 2011-2022 走看看