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
, orobject
can 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
, andextern
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的更多相关文章
- 项目中运行报错: 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 ...
随机推荐
- xmemcached的time out
最近维护线上发现不停有java.util.concurrent.TimeoutException: Timed out(200) waiting for operation的问题,排查程序.配置文件的 ...
- OpenCV初探
一种基于OpenCV的PHP图像人脸识别技术 openCV是一个开源的用C/C++开发的计算机图形图像库,非常强大,研究资料很齐全.本文重点是介绍如何使用php来调用其中的局部的功能.人脸侦查技术只是 ...
- STL内存配置器
一.STL内存配置器的总体设计结构 1.两级内存配置器:SGI-STL中设计了两级的内存配置器,主要用于不同大小的内存分配需求,当需要分配的内存大小大于128bytes时, 使用第一级配置器,否则使用 ...
- POJ 1556 The Doors(计算几何+最短路)
这题就是,处理出没两个点.假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以.最后求个最短路就可以 代码: #include <cstdio> #include < ...
- 在XAML代码中为节点树安装事件监听器
通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系. <Window x:Class="Demo002.MainW ...
- 七日筑基——C#第一天(上)
从今天开始,介绍C#如何入门,其实我们学习任何语言的目标都是为了最终能把自己想像的东西做出来,所以在最开始学习的时候要定准方向,很多人在学习过程中学着学着就变味了.比如我之前有个学生,最开始学习编程的 ...
- 在不同Activity之间传递数据的四种常用方法
在Android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法: 1.通过Intent传递数据 2.通过静态变量(static)传递数据 3.通过剪贴板(Clipboard)传递数据 4. ...
- 一般处理程序在VS2012中打开问题
问题:如果你用vs2012建立的一个一般处理程序,运行查看是,出现这样的界面 原因:VS2012默认使用IIS Web服务器,而不是Visual Studio开发服务器,基于安全考虑IIS默认不允许浏 ...
- Ubuntu_16.04 配置 Apache Rwrite URL 重写
Ubuntu Apache配置Rwrite URL重写 0. apache目录
- 虚拟机ping不通主机
centos ping不通主机 首先检查网络设备 ifconfig -a 如果有eth0 , 又存在 eth1 . 那么service eth1 stop 然后在ping主机.(以上前提是网络地址设 ...