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 33,019 of 33,346   
   Daryle Walker to All   
   Array-to-pointer decay too aggressive   
   06 May 13 09:38:25   
   
   From: darylew@googlemail.com   
      
   template < typename T, std::size_t N >   
   auto  f( T (&x)[N] ) -> T &   
   { return Whatever ? throw nullptr : x[0]; }   
      
   This is like one of those exercises with code using std::vector   
   that chokes when T is bool due to the vector having simulated   
   references instead of real ones.  In my case, having exactly one   
   action of a conditional be void (like a throw expression) gives the   
   conditional the type of the other action.  However, it's actually the   
   other action's type AFTER decay.  So array-reference returns become   
   pointers, which messes up your plans (like if "x" was a nested array).   
      
   The way around it is to force the void action to be a non-void one,   
   preferably of the same type as the other action:   
      
   template < typename T, std::size_t N >   
   auto  f( T (&x)[N] ) -> T &   
   { return Whatever ? throw nullptr, ZZZ : x[0]; }   
      
   The easiest way to get "ZZZ" to have the same type as the other action   
   is to repeat the other action ("x[0]" here).  I used a macro.  That   
   extra copy doesn't get evaluated since the throw happens first; the   
   copy is there only to change the return type of the first action.   
      
   Can we get rid of the decay rule here, or otherwise, to fix this?   
      
   Daryle W.   
      
      
   --   
         [ 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