-2

Referring to answer https://stackoverflow.com/a/19326873/10736710,

I think following should hold true:

int x; //declaration
int y = 2; //definition

But looks like first is definition and not declaration. Can someone comment how first is a definition?

2 Answers2

2

The standard (draft) says:

[basic.def]

Each entity declared by a declaration is also defined by that declaration unless:

  • (2.1) it declares a function without specifying the function's body ([dcl.fct.def]),wrong
  • (2.2) it contains the extern specifier or a linkage-specification20 ([dcl.link]) and neither an initializer nor a function-body,
  • (2.3) it declares a non-inline static data member in a class definition ([class.mem], [class.static]),
  • (2.4) it declares a static data member outside a class definition and the variable was defined within the class with the constexpr specifier (this usage is deprecated; see [depr.static_constexpr]),
  • (2.5) it is introduced by an elaborated-type-specifier ([class.name]),
  • (2.6) it is an opaque-enum-declaration ([dcl.enum]),
  • (2.7) it is a template-parameter ([temp.param]),
  • (2.8) it is a parameter-declaration ([dcl.fct]) in a function declarator that is not the declarator of a function-definition,
  • (2.9) it is a typedef declaration,
  • (2.10) it is an alias-declaration ([dcl.typedef]),
  • (2.11) it is a using-declaration ([namespace.udecl]),
  • (2.12) it is a deduction-guide ([temp.deduct.guide]),
  • (2.13) it is a static_assert-declaration ([dcl.dcl]),
  • (2.14) it is an attribute-declaration ([dcl.dcl]),
  • (2.15) it is an empty-declaration ([dcl.dcl]),
  • (2.16) it is a using-directive ([namespace.udir]),
  • (2.17) it is an explicit instantiation declaration, or
  • (2.18) it is an explicit specialization whose declaration is not a definition.

The declaration int x; isn't any of the listed exceptions, so it is a definition.

But looks like first is definition and not declaration.

It is correct that int x; is a definition as shown by the quoted rule, but it is technically wrong to say that it isn't a declaration - all definitions are declarations. You could say that it isn't merely a declaration, or you could say that it isn't a forward declaration.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • While correct, I highly doubt this is what OP is looking for. – Passer By Dec 03 '18 at 01:46
  • 2
    @PasserBy it's not really clear to me what OP is looking for. The answer to "how is `int x;` a definition?" is in fact "because the standard says it is" – M.M Dec 03 '18 at 01:47
0

Definition: when you define something it takes up actual memory in your program. When you do int x; you already take up memory for your variable even it is unitialized.

Initialization: initializing something can only be done while defining it simultaniously. The variable will already have a valid value assigned to it after creating it. E.g. int x = 3; x is now initialized and defined.

Declaration: If you declare something it does not really take up memory in your application like for example function prototypes. If your declare a function like this void foo(); it is just an introduction for the compiler and unless you define it, will not take up memory in your application.

Assignement: you assign a variable whenever you set its value after you defined it. So if you do

int x;
x = 2;

You assigned x the value 2

Yastanub
  • 1,227
  • 8
  • 19
  • I think the question is about the declaration at namespace scope – Guillaume Racicot Dec 03 '18 at 01:42
  • @GuillaumeRacicot okay nvm he accepted the answer so it seems to be helpful – Yastanub Dec 03 '18 at 01:50
  • if he understood the wrong thing, it defeat the purpose of answering – Guillaume Racicot Dec 03 '18 at 01:59
  • 1
    Note that technically, `int x;` *is* initialization. It is syntax for *default* initialization, which in case of `int` results in an indeterminate value (behaviour of the program is undefined if an indeterminate value is used). But, "uninitialized" is colloquially used for this situation, so I won't blame you for it. Also note that if the declaration is in the namespace scope, then the variable has static storage duration and therefore it would in fact be zero initialized rather than default initialized. – eerorika Dec 03 '18 at 02:10