Definitions

时间:2023-01-27 17:04:19

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.