Definitions and ODR

< cpp‎ | language
Revision as of 08:14, 19 April 2014 by D41D8CD98F (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Definitions are declarations that fully define the entity introduced by the declaration. Every declaration is a definition, except for the following:

1) 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
2) Function declaration without a function body
int f(int); // declares, but doesn't define f
3) 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;
4) 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
5) 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)
6) Declaration of a template parameter
template<typename T> // declares, but doesn't define T
7) typedef declaration
typedef S S2; // declares, but doesn't define S2 (S may be incomplete)
using S2 = S; // declares, but doesn't define S2 (S may be incomplete)
enum Color : int; // declares, but doesn't define Color
using N::d; // declares, but doesn't define d

Although asm declaration does not define any entities as well, it is a definition.

Where necessary, the compiler may implicitly define the default constructor, copy constructor, move constructor, copy assignment operator, move assignment operator, and the destructor.

If the definition of any object results in an object of incomplete type, the program is ill-formed.

[edit] One Definition Rule

Only one definition of any variable, function, class type, enumeration type, or template is allowed in any one translation unit.