0

cppreference states:

Every declaration is a definition, except for the following:

and then I do not see non-static(local) variables on the list of exceptions. However, I get why that static variables are considered as defined when declared since they are initialized to zero. BUT non-static variables are indeterminate, so are those non-static variables considered as being defined when being declared - as with static variables?

Example:

int x; // zero
int y = 0; // also zero

void foo() {
    static int x; // also zero
}

So here all the x and y are initialized to zero and therefore I think I get it if those declarations are considered as definitions.

But

void foo() {
    int x;
    printf("%d", x); // the compiler is free to crash here
}

Is the declaration of x also a definition, I mean this declaration does not really initialized to anything, right?

In fact, I have always thought that there was a pretty obvious difference between declarations and definitions, like int x;, class X; vs. int x = 2 and class X {};.

Also, if it is true that the declarations static variables also are definitions, why are static class members included in the exception list and not static variables that are not a member of a class?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 4
    definition is different thing than initialization, it is not clear why you linked them together – Slava Jun 28 '17 at 21:54
  • @Slava well okay then. However, I do not think that ruin the content of my qeustions because I still do not know whether the declarations non-static variables(not really sure about static) are definitions too? Btw. I totally forgot that definitions and initializations aren't the same thing.. I am still confused though –  Jun 28 '17 at 22:00
  • Declaration of local variables is always definition, for non local variables you can make declaration not definition by using keyword `extern` explicitly. – Slava Jun 28 '17 at 22:04
  • Plus, I do not get how declarations are definitions? As I implied in the post. –  Jun 28 '17 at 22:06
  • @Slava thanks. But exactly why is that? Are static variables defined when declared too then? –  Jun 28 '17 at 22:11
  • "how declarations are definitions?" if you write `int x;` that is declaration and definition of variable `x` of type `int`. What is not clear? – Slava Jun 28 '17 at 22:11
  • @Slava yeah okay I got you now. I think my confusion came a lot from when I mixed up definitions and initializations. One thing more though: I still do not get why static data members aren't defined then when declared in a class definition since static variables are defined by their declaration? I mean, static data members are included in the exception list –  Jun 28 '17 at 22:19
  • Though initialization sometimes turns a declaration into a definition, they are actually separate constructs. Read 3.1 Declarations and definitions (2) in this c++ standard (early) draft: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf. Maybe things get clearer then. – Stephan Lechner Jun 28 '17 at 22:27
  • 1
    @NeiLee "I still do not get why static data members aren't defined then when declared in a class definition" because it is common to put class declaration in a header file, so that declaration is visible in every translation unit which includes this header. But static data member must be defined only once. – Slava Jun 29 '17 at 13:08

0 Answers0