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,792 of 33,346   
   DeMarcus to Vidar Hasfjord   
   Re: Why is static member initialization    
   13 Jan 12 18:29:57   
   
   8b6881d9   
   From: use_my_alias_here@hotmail.com   
      
   On 2012-01-11 03:58, Vidar Hasfjord wrote:   
   > On Jan 6, 5:29 am, DeMarcus  wrote:   
   >> [About uninstantiated static member of template]   
   >> The only workaround I've found so far is the following in the   
   >> constructor of PrintingYou:   
   >>   
   >> struct PrintMe   
   >> {   
   >> PrintMe() { std::cout<<  "PrintMe"<<  std::endl; }   
   >> };   
   >>   
   >> template   
   >> struct PrintingYou   
   >> {   
   >> PrintingYou()   
   >> {   
   >> pm; // Refer to pm and hope this constructor is used.   
   >> }   
   >>   
   >> static PrintMe pm;};   
   >>   
   >> template   
   >> PrintMe PrintingYou::pm;   
   >>   
   >> I don't like this solution, first and foremost because it's unintuitive   
   >> programming. Secondly because the compiler gives:   
   >>   
   >> warning: statement has no effect [-Wunused-value]   
   >   
   > Pedantically, the warning is correct in that the execution of the   
   > statement has no effect, but overly eager in my view, since the   
   > statement has the significant effect in ensuring the instantiation of   
   > the static member. Since semantic analysis happens before code   
   > generation and optimization, the latter should ideally not affect the   
   > instantiation of the static member and hence the meaning of the   
   > program.   
   >   
   > You can use {static_cast(pm);} to silence the warning. To make   
   > the code clear, if not intuitive, you can put the static cast in a   
   > private member function EnsureInstantiationOfStaticMembers and call it   
   > from all sites that require it.   
   >   
      
   Thanks for that one Vidar!   
      
   I didn't know the standard supported "discarded-value" expressions. Actually   
   I'm still a bit confused what it means. I'll have a look at the web but if   
   anyone knows how to explain it well, please do.   
      
   I found the following paragraphs very interesting regarding static_cast.   
   It's §5.2.9/6 Static cast, and §5/10 Expressions, and §6.2/1 Expression   
   statement.   
      
   /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