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
- Any declaration with an extern storage class specifier or with a language linkage specifier(such as extern "C") without an initializer
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:
- In any translation unit, a
template, type,function, orobjectcan have no more than one definition. Some of these can have any number of declarations. A definition provides an instance. - 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.
- Some things, like types,
templates, andexterninline 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的更多相关文章
- 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]
记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...
- 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 ...
- [WebService] the namespace on the "definitions" element, is not a valid SOAP version
公司对外通过webservice访问别人接口,对方webservice IP地址发生变化,切换过去之后,始终报错,在网上搜索了各种办法之后,暂时总结该问题几种可能解决办法,待真正解决时用的到. 异常详 ...
- 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 ...
- PeopleSoft Object Types Definitions
PeopleSoft stores object definitions types such as Record, Field and SQL definitions as numbers in ...
- Hex-Rays decompiler type definitions and convenience macros
/****************************************************************************************** Copyrigh ...
- Circular placeholder reference 'server.port' in property definitions
Exception in thread "main" java.lang.IllegalArgumentException: Circular placeholder refere ...
- 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 ...
- Circular placeholder reference 'jdbc.driver' in property definitions
Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'jdbc.driver' in prope ...
随机推荐
- oracle 配置监听
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521 ...
- josscrowcroft / Simple-JavaScript-DOM-Inspector
josscrowcroft / Simple-JavaScript-DOM-Inspector josscrowcroft / Simple-JavaScript-DOM-Inspector
- 每天学点Linux:二
关于输入输出和重定向: 默认情况下标准I/O的输入为键盘,输出为显示屏.输入和输出可以通过符号‘>’进行重定向. 例如可以通过命令:$>newfile 来创建一个新文件(如果newfile ...
- svn中的Trunk,branches,tags深度理解
trunk.就是主干,这个目录以下直接放源代码了,我们创建项目的时候,把项目源代码放到这个目录.import进svn branches.就是分支,以下可能有非常多trunk,比方trunk_1_0_1 ...
- 沙朗javascript(两)正则表达式
以下提到的文章javascript基础,今天我说些什么javascript正则表达式正确. 说到这首先要问了,什么是正則表達式,正則表達式能干什么? 正則表達式:一个正則表達式就 ...
- 七日筑基——C#第二天
上一次讲到了变量,变量这个东西可以说是编程的基础,主要的作用就是用来存放数据,就跟做菜一样的,不同的菜要放在不同类型的容器中,那么不同的数据也需要存放在不同类型的变量里.先放张饭菜的图给大家看看,增加 ...
- SSIS:捕获修改了的数据
获取修改了的数据一般有三种方式: 1.使用一个datetime列 缺点:是并不是每个表都会有个‘修改日期’字段来让你判断行是否修改过 使用实例可以参考我之前的文章:SSIS: 使用最大ID和最大日期来 ...
- CTRL+A, CTRL+C, CTRL+V
(http://leetcode.com/2011/01/ctrla-ctrlc-ctrlv.html) Imagine you have a special keyboard with the fo ...
- 加装 ImageMagick 性能更佳!
1. 下载 Download ImageMagick 以此文件ImageMagick-6.9.1-10-Q16-x64-dll-win进行,第二次开发的研发 2. 安装 Install ImageMa ...
- Oracle分区知识
查询分区名称.表空间的SQL USER_SEGMENTS SELECT SEGMENT_NAME,PARTITION_NAME,TABLESPACE_NAME FROM USER_SEGMENTS; ...