##What is “Closure”
In computer science, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables) of that function. A closure unlike a plain function pointer allows a function to access those non-local variables even when invoked outside of its immediate lexical scope.
an example in python 3:
The closure returned by counter can be assigned to a variable:
Invoking increment through the closures will give the following results:
The closure and anonymous function are distinct concepts. A closure retains a reference to the environment at the time it was created (for example, to the current value of a local variable in the enclosing scope) while a generic anonymous function need not do this.
C++11 with the lambda expression partially support closure – the programmer chooses whether the lambda makes copies of the captured stack variables or only references them. References will cause UndefinedBehavior if the lambda escapes the scope it was created in. That is because C++ run-time memory model allocates all local variables on a linear stack. A function’s local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function’s execution. So typically, languages that natively support closures also use garbage collection.
###basic structure of c++11 lambda expression:
 Capture nothing (or, a scorched earth strategy?) [&] Capture any referenced variable by reference [=] Capture any referenced variable by making a copy [x, &y] Capture x by making a copy, but capture y by reference [=, &foo] Capture any referenced variable by making a copy, but capture variable foo by reference [bar] Capture bar by making a copy; don't copy anything else [this] Capture the this pointer of the enclosing class
Closure types not specified, but two easy ways to store closures:
RAII stands for Resource Acquisition Is Initialization, was invented by Bjarne Stroustrup to deal with resource allocation and deallocation in C++. The idea is that you want to acquire resources during the initialization of objects, i.e. as soon as possible, so that you cannot accidentally use an uninitialized object, and also that you want your object to automatically release the resource upon destruction. One of the main advantages of this pattern is that your resources will always be released, even if there are errors or exceptions between when your object is initialized and your object goes out of scope.