0

See "Note" on second page: enter image description here Page 2

This convenience gave rise to serious inconsistencies and was removed several years ago

The book was authored in 2003.

  • What exactly does it mean by "Serious inconsistencies"?
  • Was a compiler flag for backwards compatibility created?
  • Did this behaviour also exist in C?
Anon
  • 3,565
  • 3
  • 27
  • 45
  • 2
    Please, post text as text, not as photographs of text. This is a website for software engineers, not photographers. We want to read your text, not critique your use of color and perspective. – Jörg W Mittag Jan 20 '23 at 09:36
  • 1
    I feel that terms like "second declaration", "redeclaring" and "a is declared both inside shared and outside of it" muddy the waters. There is exactly one declaration, which is within the class definition; and one declaration, which is outside of the class definition (very often the definition will be in a header file, while the declaration is in the corresponding cpp file). – Roel Schroeven Jan 20 '23 at 12:41

1 Answers1

6
  • What exactly does it mean by "Serious inconsistencies"?

I don't know what the author meant, but static variables (in classes) need to be assigned to one and only one compilation unit. One usually puts the class declaration in a header file, which can be included in multiple cpp files, the compilation units. Without a second declaration outside the class declaration, the compiler would not know to which compilation unit the variable belongs. Trying to let the compiler make such a decision automatically is pretty hard, since traditional C++ compilers translate compilation units independently from each other. So a compiler would have to "reserve" some space tentatively in each object file, and the linker had to resolve it. Not impossible, but I can imagine this to cause a lot of trouble when the automatic decision about the assigned compilation changes from one build run to the next.

  • Was a compiler flag for backwards compatibility created?

There are several C++ compilers available all over the world, and compiler flags change potentially with each new version. If you are interested in the flags for a specific compiler, please RTFM.

Actually, I am using C++ since the early 1990s, and I cannot remember ever to have worked with a compiler where a class-static variable did not require a separate declation within a cpp file. So this syntax is probably deprecated since three decades, which makes me wonder if there is any real necessity to support a "feature" which never seemed to have worked well and probably caused more trouble than benefits.

  • Did this behaviour also exist in C?

In C there are no classes, so not literally.

The nearest equivalent of a C++ class-static variable in C is a global variable which is declared in one ".c" file, together with an "extern" declaration for the same variable (often, but not necessarily) placed in some header file. Including the header file will allow other modules to access the shared variable. So yes, a global variable in C has also to be declared twice, once at the compilation unit which "stores" it, and once in a header file to make it accessible for others.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • "raditional C++ compilers translate one compilation unit individually from each other" pressed enter too early, sorry. Nowadays, with modules, afaik there's a need for communication anyway. Makes one wonder how this would affect the choice. – jaskij Jan 20 '23 at 21:12