Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,808 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to DeMarcus    |
|    Re: When is the non-local static variabl    |
|    16 Jan 12 13:08:37    |
   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-01-16 00:15, DeMarcus wrote:   
   > I have some problems interpreting the C++11 standard. I can't figure out   
   > where in the standard it defines at what time the constructor of a   
   > non-local static variable is run.   
      
   [..]   
      
   > Let's say I have this program.   
   >   
   > #include "SomeClass.hpp"   
   >   
   > SomeClass sc;   
   >   
   > int main()   
   > {   
   > // Here we run a lot of code without accessing sc.   
   > // [...]   
   >   
   > std::cout << "Shutting down" << std::endl;   
   >   
   > sc.someFunction();   
   >   
   > return 0;   
   > }   
   >   
   > As I interpret §3.6.2/1 it means the constructor of sc should be run at   
   > "some" point in the start-up of the application.   
   >   
   > But as I interpret §3.6.2/4 it means that the compiler is free to run   
   > the constructor more or less whenever it wants but before the   
   > application shuts down.   
   >   
   > When can I expect the constructor to be run?   
      
   Given your example, I interpret your question in the following way:   
      
   "May the constructor of SomeClass be evaluated *after* the observable output   
   in main() described by   
      
   std::cout << "Shutting down" << std::endl;"   
      
   Anything else is hard to reflect upon given your comments in the initial lines   
   of the main() function.   
      
   Even though the library provides some specific guarantees about IO and   
   initialization - specifically [iostream.objects.overview] p2 describes that an   
   ios_base::Init object of static storage is defined in the same translation   
   unit as sc - I cannot find    
   wording that would forbid the aforementioned output to occur *before* the   
   construction of the sc object in a deferred implementation.   
      
   As described in your quote of [basic.start.init] p4 you have to look for   
   variables and functions defined in your translation unit (TU) that are   
   odr-used in this TU to argue about order of constructions.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca