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 ...
随机推荐
- [LeetCode]题解(python):137-Single Number II
题目来源: https://leetcode.com/problems/single-number-ii/ 题意分析: 给定一个数组,数组里面每一个数字都出现了3次除了一个,找出那个数.要求时间复杂度 ...
- java调用C++ DLL库方法
最近一个项目要开发网页端人脸识别项目,人脸识别的算法已经写好,是C++版,但是网页端要求使用Java后台,这就涉及到Java调用DLL的问题.经过查找,实现了一个简单的例子. 1.第一步,先在Java ...
- delete了,析构函数却没有调用
析构函数在对象的生命结束时,会自动调用,大家所熟知的智能指针就是根据析构函数的这种特性而实现的,包括Qt的内存管理机制,也都是利用了析构函数的这一机制来实现的.c++创始人Bjarne Stroust ...
- javascript集合求交集
两集合求交集 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以 ...
- 夜未央Test1
积木游戏(block.pas) [题目描述] 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,最高的积木的最终需要达到h. 在搭建开始之前,没有任何积木(可以看成n ...
- iOS开发中,应用内直接跳转到Appstore
iOS开发中,应用内直接跳转到Appstore 1.进入appstore中指定的应用NSString *str = [NSString stringWithFormat: ...
- To the Max(矩阵压缩)
To the Max Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) Total Su ...
- oracle decode函数用法
DECODE函数是ORACLE PL/SQL是功能强大的函数之中的一个,眼下还仅仅有ORACLE公司的SQL提供了此函数,其它数据库厂商的SQL实现还没有此功能.DECODE有什么用途 呢? 先构造一 ...
- Fastboot的使用简单教程
大家都知道HTC手机重新启动进入所谓的project模式,就是HBOOT,然后能够进入FASTBOOT界面,在这个界面.我们能够在电脑端能够做非常多事,特别是HBOOT被改动过,假设是ENG S-OF ...
- How draw a stem -and -leaf & box-plot display by R.or Python
参考: 使用R进行数据可视化套路之-茎叶图.盒形图 step by step R 读取数据 在网上下载的2013全国各省区GDP排名(exl文件) 先 另存为 data.txt(为了方便存到D盘文件夹 ...