1

After solving this simple issue, I had to ask :

-> In the H file in a class ex a const static member is defined, e.g. :

class ex {
    const static int my_ex;
};

-> In the CPP file the value is specified

ex::my_ex = 32;  

And then one gets the error "conflicting declarations" (as well as "does not name a type"). I understand that the definition in the CPP file is also a declaration which does create a conflict seen from the linker BUT why only about the const specifier (and type) and not the static one ? I only have to write

const int ex::my_ex = 32;

to get it to compile. But not static ... Why not? Why can't I just define and not repeat declaration related steps (type, specific identifiers)?

Community
  • 1
  • 1
C. THIN
  • 147
  • 3
  • 11
  • 1
    `static` never is used for definitions. – πάντα ῥεῖ Apr 18 '16 at 15:03
  • Ok... but **why** is really my question ... – C. THIN Apr 18 '16 at 15:05
  • Static in declaration and static in definition mean different thing. – SergeyA Apr 18 '16 at 15:05
  • @πάνταῥεῖ this is not technically correct. `struct X { static const int x = 42; };` – SergeyA Apr 18 '16 at 15:06
  • Everything typed entity has precisely one type. You can't specify different types for the same entity at different times. `static` is different because it's not part of the type. – Kerrek SB Apr 18 '16 at 15:06
  • @SergeyA OK, inline definitions aside. – πάντα ῥεῖ Apr 18 '16 at 15:06
  • @SergeyA: That's not a definition. That's a declaration and an initializer. – Kerrek SB Apr 18 '16 at 15:07
  • @SergeyA Correct actually, but it doesn't really answer the question. If they decided to not force the writer to rewrite the word static, then they could also have done the same for the word const ... – C. THIN Apr 18 '16 at 15:07
  • The question is really not very different from why you can't declare `extern int a; extern char a;` – Kerrek SB Apr 18 '16 at 15:08
  • You're all answering a little fast, would someone sum up definition vs. declaration vs. initialization vs. type-related keywords vs. other keywords vs. meaning of keywords depending on context ? That'd be great. – C. THIN Apr 18 '16 at 15:09
  • 2
    Sounds like you're asking for a description of how C++ works. Here you go: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Apr 18 '16 at 15:10
  • You're either asking why `const` needs to be given again, or why `static` _doesn't_. (Or why there's an inconsistency.) Which is it? And why? What problem are you trying to solve? – Lightness Races in Orbit Apr 18 '16 at 15:11
  • @KerrekSB yes, I was wrong. But in a more correct example with static member function with inline definition static would still be used for definition. – SergeyA Apr 18 '16 at 15:14
  • "Why can't I just define and not repeat declaration related steps" — in my opinion, repeating specifiers is useful: reading a code is more convenient, you have more important information about members without opening a header. But it's somewhat inconsistent that you can't put static for a definition bc of contradicting with another semantics. – Oleksa Jul 15 '20 at 08:15

1 Answers1

9

This is a historical thing.

Since C, static on a definition means "internal linkage". When C++ came along, and classes were added to it, Bjarne needed a keyword to signify static members. Not wanting to add new keywords (a preference that largely exists still to this day), he re-used static instead.

Now static meant two different things depending on where you put it. So you can't write static here because it would mean something else. Thus, the language doesn't require you to, as that would be silly.

Beyond that reasoning, it just is. When you create a language, you balance simplicity of specification against simplicity of implementation against simplicity of use, and you come up with a set of rules that are the language. At some point you have to stop quibbling over "why" some insignificant rule was created and just get on with writing your program.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055