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,565 of 33,346   
   Marc to Sarang   
   Re: constexpr, lambdas and arrays on sta   
   13 Oct 11 16:47:13   
   
   4b88bcf9   
   From: marc.glisse@gmail.com   
      
   Sarang  wrote:   
      
   > Hello,   
   >   
   > I am trying out constexpr, and was trying to create an array on stack of   
   length 20, but with constexpr the value is not hardcoded but evaluated at   
   compile time to create array on stack!   
   >   
   >   
   > here is the code:   
   >   
   > template   
   > constexpr T scale(T num, T factor)   
   > {   
   >    return ([=](){return num*factor;})();   
   > }   
   >   
   > int main()   
   > {   
   >    int arr[scale(10,2)];   
   >    auto numElems = sizeof(arr)/sizeof(arr[0]);   
   >    return 0;   
   > }   
      
   [expr.const] says that "a conditional-expression is a core constant   
   expression unless it involves one of the following as a potentially   
   evaluated subexpression [...] a lambda-expression".   
      
   That's not to say there aren't cases where it might have made sense to   
   allow it, but it wasn't done. So the constexpr on scale is ignored.   
   And the VLA extension of your compiler kicks in.   
      
   Still, I would have expected that with a high enough level of   
   optimization the lambda and scale would be inlined.   
      
      
   --   
         [ 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