Timeline for Why the static data members have to be defined outside the class separately in C++ (unlike Java)?
Current License: CC BY-SA 3.0
11 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 20, 2013 at 10:09 | comment | added | ixache | @Klaim/C++ compilation model: I think than Emilio's answer is trying (and succeeding in parts) to precisely explain the C++ compilation model, even it could (still) be improved. | |
Nov 18, 2012 at 11:47 | comment | added | han | @iammilind: Templates are typically instantiated in every object file, including their static variables. On Linux with ELF object files, the compiler marks the instantiations as weak symbols, which means that the linker combines multiple copies of the same instantiation. The same technology could be used to allow defining static variables in header files, so the reason it isn't done is probably a combination of historical reasons and compilation performance considerations. The entire compilation model will hopefully be fixed once the next C++ standard incorporates modules. | |
Apr 20, 2012 at 16:49 | comment | added | mjfgates | The standard may have changed, but as of 2000, a templatized class with static members had to have the statics defined in a .cpp file somewhere for each specialization, leading to variable definitions like "int Foo<int>::bar;" in client code. Which is one of the reasons why most people avoided putting statics in their templates. | |
Apr 20, 2012 at 16:10 | comment | added | Klaim | @Giorgio Well for each instantiation it will generate code (less code if it's twice the same instantiation) that you will not see and that will have the one unique definition, so as I said it is "orthogonal". You have to define the template definition of the static object, but the compiler will put it somewhere for you, as it is a template, and not "real code". | |
Apr 20, 2012 at 15:17 | comment | added | Giorgio | @Klaim: I would be curious to know what happens if I have one template <class T> ..., containing a static variable and I instantiate it twice in the same .cpp file using the same class for the parameter T. How is this case handled? | |
Apr 20, 2012 at 9:47 | comment | added | Klaim | This answer would benefit a link to a full explanation of the c++ compilation model, that is one of these C++ knowledge that most developers dont understand until you explain it fully. | |
Apr 20, 2012 at 9:46 | comment | added | Klaim | @iammilind templates are not real code, they are code that generate code. Each instance of a template have one and only one static instance of each static declaration that is provided by the compiler. You still have to define the instance but as you define a template of an instance, it is not real code, as said above. Templates are, literally, templates of code for the compiler to generate code. | |
Apr 20, 2012 at 9:16 | comment | added | iammilind |
@Klaim, what about static members in template ? They are allowed in all the header files as they need to be visible. I am not disputing this answer, but it doesn't match my question also.
|
|
Apr 20, 2012 at 9:10 | comment | added | Klaim | @iammilind I think you don't understand that the grammar is necessary because of the explaination of this answer. Now why? Because of the compilation model of C (and C++): c and cpp files are the real code file that are compiled separately like separate programs, then they are linked together to make a full executable. The headers are not really code for the compiler, they are only text to copy and paste inside c and cpp files. Now if something is defined several times, it cannot compile it, the same way it will not compile if you have several local variables with the same name. | |
Apr 20, 2012 at 8:34 | comment | added | iammilind |
My question is not the reason for current behavior, but rather the justification for such language grammar. In other words, suppose if static variables are declared/defined at the same place (like Java) then what can go wrong ?
|
|
Apr 20, 2012 at 5:24 | history | answered | mjfgates | CC BY-SA 3.0 |