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. 共享内存(shared memory)

    共享内存指在多处理器的计算机系统中,可以被不同中央处理器(CPU)访问的大容量内存.由于多个CPU需要快速访问存储器,这样就要对存储器进行缓存(Cache). 任何一个缓存的数据被更新后,由于其他处理 ...

  2. What do `?i` and `?-i` in regex mean?

    http://stackoverflow.com/questions/15145659/what-do-i-and-i-in-regex-mean

  3. .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML

    原文http://www.cnblogs.com/mgen/archive/2011/05/24/2056025.html 众所周知,XmlDocument可以进行XPath查询,但实际上这里所说的X ...

  4. C++的类型萃取技术

    应该说,迭代器就是一种智能指针,因此,它也就拥有了一般指针的所有特点——能够对其进行*和->操作.但是在遍历容器的时候,不可避免的要对遍历的容器内部有所了解,所以,设计一个迭代器也就自然而然的变 ...

  5. HDU 5828 Rikka with Sequence(线段树)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5828 [题目大意] 给出一个数列,要求支持区间加法,区间开方和区间和查询操作. [题解] 考虑开方 ...

  6. 有道翻译API

    轻奢侈品_百度百科 轻奢侈品 有道翻译API 有道翻译API申请成功 API key:72763558 keyfrom:lexus-studio

  7. CodeForces 160D - Distance in Tree 树型DP

    题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...

  8. asp.net 连接access数据库方法

    在 Web.Config 中配置 Access 数据库驱动和数据库文件名称. 请看代码 <appSettings> <add key="DBDriver" val ...

  9. :before和:after的内幕以及伪类

    pseudo-classes vs pseudo-elements http://m.blog.csdn.net/blog/zhuizhuziwo/7897837

  10. php mysql实现栏目分类递归

    header("content-type:text/html;charset=utf-8"); $dbhost = "localhost";   // 数据库主 ...