From: use_my_alias_here@hotmail.com   
      
   > 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]   
   >   
   > Third; I have no idea what the compiler is capable of. Maybe it decides   
   > to optimize away that unused statement and the construction of PrintMe   
   > with it.   
   >   
      
   With Vidar Hasfjord's proposal it feels that the above solution is the   
   only proper workaround until the standard has been fixed to provide an   
   alternative. The slight change would be the following in the constructor   
   of PrintingYou.   
      
   PrintingYou()   
   {   
    // Refer to pm and hope this constructor is used.   
    static_cast(pm);   
   }   
      
   Hopefully this eliminates some of my worries;   
   1. It's still unintuitive programming though.   
   2. The warning correctly disappears.   
   3. I still have no idea what the compiler is capable of but with the   
    static_cast I feel more secure the compiler understands that the   
    expression shall not be optimized away.   
      
   However, I now have a fourth worry; will there be any code inserted   
   using this statement? I definitely don't want to add any extra   
   instruction everywhere this is used.   
      
   I compiled the code with- and without the pm expression and looked at   
   the assembly and both gave me exactly the following for the constructor:   
      
   g++ -S pm.cpp   
      
   _ZN11PrintingYouIiEC2Ev:   
   .LFB975:   
    .cfi_startproc   
    pushq %rbp   
    .cfi_def_cfa_offset 16   
    .cfi_offset 6, -16   
    movq %rsp, %rbp   
    .cfi_def_cfa_register 6   
    movq %rdi, -8(%rbp)   
    popq %rbp   
    .cfi_def_cfa 7, 8   
    ret   
    .cfi_endproc   
      
   Since there was no difference between the two I expect that the   
   static_cast(pm) does nothing to the execution time of the constructor.   
      
   Is my analysis above correct?   
      
   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)   
|