Although recursive Lambda can reduce the execution efficiency by adding external side effects, it is useful in many cases.
Lambda generally cannot be recursive. In most scenarios that support Lambda featuresProgramming LanguageBoth.
The following is a simple example:
# Include <iostream> <br/> # include <typeinfo> <br/> # include <functional> <br/> using namespace STD; <br/> int main (void) <br/>{< br/> STD: function <int (INT)> product; <br/> product = [& product] (int n) -> int {return n <= 1? 1: N * product (n-1) ;}; <br/> cout <"The answer is:" <product (5) <Endl; <br/>}< br/>
AboveCodeMicrosoft's Visual C ++ 2010 express edition and gcc4.5.0 are both compiled and run. The results are correct.
Note that, if you use the GCC compiler of mingw, you need to add-STD = C ++ 0x in the C ++ compilation option; in addition, you must add-static-libstdc ++ to the connector options to use the static Runtime Library of C ++.