Search code examples
c++cone-definition-rule

Why One Definition Rule, not One Declaration Rule?


I have read materials below:

https://www.wikiwand.com/en/One_Definition_Rule

http://en.cppreference.com/w/cpp/language/definition

What is the difference between a definition and a declaration?

But still, can't figure out why it is One Definition Rule rather than One Declaration Rule?

I maintain that declaration is a subset of definition, so One Definition Rule is enough.


Solution

  • Definition is a subset of declaration, not the other way around. Every definition is a declaration, and there are declarations that are not definitions.

    int i = 3;      // definition and declaration
    extern int i;   // ok: (re)declaration
    int i = 4;      // error: redefinition
    
    extern int j;   // declaration
    extern int j;   // ok: (re)declaration
    int j = 5;      // ok: (re)declaration and definition
    int j = 6;      // error: redefinition