home bbs files messages ]

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,803 of 33,346   
   DeMarcus to All   
   When is the non-local static variable co   
   15 Jan 12 15:15:36   
   
   From: use_my_alias_here@hotmail.com   
      
   Hi,   
      
   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. I have found the following.   
      
   §3.6.2/1 says "... Non-local variables with static storage duration are   
   initialized as a consequence of program initiation."   
      
   §3.6.2/4 says "It is implementation-defined whether the dynamic   
   initialization of a non-local variable with static storage duration is   
   done before the first statement of main. If the initialization is   
   deferred to some point in time after the first statement of main, it   
   shall occur before the first odr-use (3.2) of any function or variable   
   defined in the same translation unit as the variable to be initialized."   
      
   and by footnote 34 it adds   
      
   "A non-local variable with static storage duration having initialization   
   with side-effects must be initialized even if it is not odr-used"   
      
   §3.7.1/2 says "If a variable with static storage duration has   
   initialization or a destructor with side effects, it shall not be   
   eliminated even if it appears to be unused, except that a class object   
   or its copy/move may be eliminated as specified in 12.8"   
      
      
   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?   
      
      
   Thanks,   
   Daniel   
      
      
   --   
         [ 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