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,774 of 33,346    |
|    Dave Harris to mark.st.lee@gmail.com    |
|    Re: Lambda allocations    |
|    03 Jan 12 10:20:21    |
   
   4cfdfb23   
   From: brangdon@cix.co.uk   
      
   mark.st.lee@gmail.com (rgba32) wrote (abridged):   
   > Lambdas are a new-ish topic for me.   
      
   Me too. I was slightly involved very early on, but lost track for a few   
   years. Only recently have I started using a compiler that supported them.   
      
      
   > As I dive in an learn more about them, it occurred to me that   
   > there may be some "hidden" memory allocations happening to account   
   > for the cases where a closure and it's state need to outlive the   
   > scope it was created in.   
      
   My current understanding is that that isn't lambdas, that's   
   tr1::function<>. They are often used together.   
      
   A lambda expression is a convenient way of writing a class. The type of   
   the expression is the type of the class, and only known to the compiler.   
   The class doesn't inherit from anything and doesn't offer any (run-time)   
   polymorphism. This means that at low level, lambdas are used with   
   templates or auto in ways that capture the full type. For example:   
      
    auto x = []( int z ) { return z+1; }   
    auto y = []( int z ) { return z+1; }   
      
   here x and y are different objects with different types. And because of   
   this, they don't need dynamic allocation. The size of a lambda is always   
   known at compile time and so can be allocated on the stack. If it   
   captures variables by value or by reference, the requisite size is also   
   known at compile-time so it still doesn't need heap allocation.   
      
   If you want to use lambdas dynamically, then you should probably use   
   tr1::function<>. Consider:   
      
    template
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca