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.

Definitions的更多相关文章

  1. 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]

    记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...

  2. Formal Definitions Using ASN.1 - SNMP Tutorial

    30.7 Formal Definitions Using ASN.1 The SMI standard specifies that all MIB variables must be define ...

  3. [WebService] the namespace on the "definitions" element, is not a valid SOAP version

    公司对外通过webservice访问别人接口,对方webservice IP地址发生变化,切换过去之后,始终报错,在网上搜索了各种办法之后,暂时总结该问题几种可能解决办法,待真正解决时用的到. 异常详 ...

  4. The repository for high quality TypeScript type definitions

    Best practices This is a guide to the best practices to follow when creating typing files. There are ...

  5. PeopleSoft Object Types Definitions

     PeopleSoft stores object definitions types such as Record, Field and SQL definitions as numbers in  ...

  6. Hex-Rays decompiler type definitions and convenience macros

    /****************************************************************************************** Copyrigh ...

  7. Circular placeholder reference 'server.port' in property definitions

    Exception in thread "main" java.lang.IllegalArgumentException: Circular placeholder refere ...

  8. source install MacPorts--checking for Tcl configuration... configure: error: Can't find Tcl configuration definitions

    If you installed MacPorts using the package installer, skip this section. To install MacPorts from t ...

  9. Circular placeholder reference 'jdbc.driver' in property definitions

    Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.driver' in prope ...

随机推荐

  1. td之overflow:hidden 多余文本隐藏效果

    td之overflow:hidden 多余文本隐藏效果 方法1: table-layout: fixed; width: 200px; 语法: table-layout : auto | fixed ...

  2. RHEL6.4 NFS文件共享服务搭建

    NFS文件共享服务 1 实验方案 使用2台RHEL6.4虚拟机,其中一台作为NFS共享服务器(192.168.100.1).另外一台作为测试用的NFS客户机(192.168.100.2) 2.实现 2 ...

  3. makefile简单helloworld

    最近要在unix系统上开发c++应用程序,但默认情况下unix编译c++程序需要使用makefile.其实makefile语法还是比较简单,看上去有点像ant.废话不说了,直接上helloworld. ...

  4. mac outlook无法发送邮件

    工具-帐户 第一步把SSL钩挑上 第二步 下面的更多选项,验证选择“使用接收服务器信息” 搞定了!记得个赞!

  5. 在C语言中基本数据类型所占的字节数

    基本数据类型所占的字节数其实跟C语言本身没有太大的关系,它取决于编译器的位数,下面这张表说明了不同编译器下基本数据类型的长度: 32位编译器中各基本类型所占字节数: 注:对于32位的编译器,指针变量的 ...

  6. jbpmAPI-5

    Part II. jBPM Core 5.1.概述本章介绍了API需要加载过程和执行它们.更多的细节如何定义过程本身,看看在BPMN 2.0章.与流程引擎交互(例如,开始一个过程),你需要建立一个会话 ...

  7. codeforces 609F. Frogs and mosquitoes 二分+线段树

    题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...

  8. SIF与CIF

    SIF 动态图像专家组(MPEG)在1992年推出的MPEG-1标准中首次定义了SIF(Source Input Format,源输入格式).CCIR 601标准(现改为ITU-R BT.601标准) ...

  9. c风格字符串函数

    十一.C 风格字符串  1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat(p, p1) 附加字符串  strncat( ...

  10. spss

    编辑 SPSS(Statistical Product and Service Solutions),“统计产品与服务解决方案”软件.最初软件全称为“社会科学统计软件包” (SolutionsStat ...