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. ubantu root 默认密码

    安装完Ubuntu后忽然意识到没有设置root密码,不知道密码自然就无法进入根用户下.到网上搜了一下,原来是这麽回事.Ubuntu的默认root密码是随机的,即每次开机都有一个新的root密码.我们可 ...

  2. Codeforces Round#2

    A. Winner 题目大意:一些人在玩游戏,给出n次某人获得的分数,现在请计算出胜者,如果不止一个最高分,则取最先达到该分数的人. 题解:模拟得分过程,首先计算出最高分,然后获取先到达或超过最高分的 ...

  3. c++程序内存泄露检測工具

    功能: 用于检測c++程序的内存泄露. 原理: 事实上非常easy,就是通过函数的重载机制,捕获应用程序的new, new[] , delete , delete[], malloc,calloc,f ...

  4. Bzoj2034 2009国家集训队试题 最大收益 贪心+各种优化+二分图

    这个题真的是太神了... 从一開始枚举到最后n方的转化,各种优化基本都用到了极致.... FQW的题解写了好多,个人感觉我全然没有在这里废话的必要了 直接看这里 各种方法真的是应有尽有 大概说下 首先 ...

  5. SQL(Oracle)日常使用与不常使用函数的汇总

    --日常使用的sql语句和oracle语句,有些相对使用的频率比较高,收藏起来还是比较值得的 -- 绝对值 SQL:) value Oracle:) value from dual -- 2.取整(大 ...

  6. Structs

    1.服务端的运行程序 2.Servlet的三个方法 init service:抽象方法 destroy 3.步骤 (1).在web.xml中 <servlet> <servlet-n ...

  7. PHP自练项目之数字分页效果

    学习要点:1.LIMIT 用法2.各种参数3.超链接调用 第一:先在文件中设置数字分页模块:我的文件是(blog.php) //分页模块 $_page = $_GET['page']; $_pages ...

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

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

  9. 通过读取excel数据和mysql数据库数据做对比(二)-代码编写测试

    通过上一步,环境已搭建好了. 下面开始实战, 首先,编写链接mysql的函数conn_sql.py import pymysql def sql_conn(u,pwd,h,db): conn=pymy ...

  10. 怎么给没链接的flash加超链接

    最近开始准备设计一个广告条,本想用阿里妈妈的的banner marker来设计,却遗憾的发现,banner marker已经实行收费模式了. 我不得不启用另一款在线banner生成工具,百度旗下的&q ...