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. ny 58 最少步数 (BFS)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=58 就是一道简单的BFS 练习练习搜索,一次AC #include <iostream& ...

  2. .net mvc笔记2_Essential C# Features

    Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...

  3. underscore api 概览

    underscore 集合函数(数组或对象) _.each(list, iteratee, [context]); _.map(list, iteratee, [context]); _.reduce ...

  4. 强化:把treeview的QString路径转换为QModelIndex节点,有了节点就什么都好办了

    http://doc.qt.io/qt-4.8/qdirmodel.html#index-2 甚至还能直接调用setData: setData(const QModelIndex &index ...

  5. CURD 例子

    public function modify(){ $id=$_GET['id']; $m=M('user'); $arr=$m->find($id); //var_dump($arr); $t ...

  6. 预处理指令中#Pragma

    在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#pragma指令对每个编译器给出了一个方法,在保持与C和C++语言完全兼容的 ...

  7. zoj 1409 Communication System

    /*如果要一个物体的多种属性,最好用结构体,不要用二维数组或者多维数组.用多维数组进行关键字排序很不稳定 */ /*给每个设备的所有价格排序,每个设备选取恰好比已知带宽大的价格(这个时候的比例最大) ...

  8. 谈如何使用c中的qsort快速排序库函数 按主次关键字正确排序

    快排的效率很快,但是我们很少知道如何利用它进行多关键字排序,比如我想对一个数组a[i][0]进行的一个元素进行主关键字排序,又想对a[i][1]进行次关键字排序.那么接下来就是解决这个问题的方法. 学 ...

  9. Foundation NSMutableArray遍历,选取出符合条件的所有对象

    一.查找数组中一个元素,找到后立即返回 当遍历数组只需要返回其中一个符合条件的元素时,使用 indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, ...

  10. JavaSE_ 多线程 总目录(23~24)

    JavaSE学习总结第23天_多线程123.01 多线程程序的引入23.02 进程概述及多进程的意义23.03 线程概述及多线程的意义23.04 并行和并发的区别23.05 Java程序运行原理和JV ...