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 ...
随机推荐
- 身份验证cookies和Token
后端服务器有两种基本的身份验证:1.是基于Cookie的身份验证,使用服务器端的cookie来对每次请求的用户进行身份验证.2. 较新的方法,基于令牌Token的认证,依赖于被发送到服务器上每个请求的 ...
- php环境配置优化
Php相关配置 – 基础 max_execution_time = 30 max_input_time = 60 memory_limit = 128 Mmax_input_vars = 1000 r ...
- linux下解压iso文件
.iso文件的格式是iso9660,iso9660是cd上的一种文件系统, 也就是说是 是数据在cd上的组织形式: 它的一些限制是: 1.最多8级子目录(可以用RockRidge Extension增 ...
- Objective-C中NSArray和NSMutableArray的基本用法
/*---------------------NSArray---------------------------*/ //创建数组 NSArray *array1 = [NSArray arrayW ...
- android解决内存溢出的问题(没有从根本上解决)
Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...
- 将Oracle数据库导出为txt格式
将Oracle数据库导出为txt格式: 方法1: 对于Windows系统,可以采用以下方式: 选择控制面板-->管理工具-->数据源(ODBC),添加一个新的数据源(系统或用户DSN均可) ...
- C#隐藏桌面图标和任务栏
最近因为项目需要需要实现桌面图标和任务状态栏的隐藏功能,实现的方式很多,比如修改注册表值,调用windows API函数等.经过一番的查阅,这个功能暂时实现了,或许不是很好的方法,但是我预期的效果达到 ...
- 1.3. chromium源代码分析 - chromiumframe - 窗口系列
在_tWinMain中有这样两条语句: MainWindowDelegate delegate; view::Window::CreateNativeWindow(NULL, gfx::Rect(), ...
- UNIX/Linux-进程控制(实例入门篇)
UNIX进程 进程标识符 要想对进程控制,必须得获取进程的标识.每个进程都有一个非负整数表示的唯一进程ID,虽然是唯一的,但是进程ID可以重用.当一个进程终止后,其进程ID就可以再次使用了. 系统 ...
- web附件中文名
response.setHeader("Content-Disposition", "attachement;filename="+URLEncoder.enc ...