C++完成的一個可以寫遞歸lambda的Y函數。本站提示廣大學習愛好者:(C++完成的一個可以寫遞歸lambda的Y函數)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成的一個可以寫遞歸lambda的Y函數正文
比來進修C++11的variadic template argument,終究可以解脫用fpmacro模板來復制一年夜堆代碼的做法了,好高興。這個例子的main函數用lambda寫了一個斐波那契數列的遞歸盤算函數。跟以往分歧的是,在Y函數的贊助下,這個lambda表達是可以勝利看到本身,然後遞歸挪用。固然這依然須要用通俗的C++遞歸來完成,其實不是λ-calculus誰人嵬峨上的Y Combinator。
#include <functional> #include <memory> #include <iostream> #include <string> using namespace std; template<typename TResult, typename ...TArgs> class YBuilder { private: function<TResult(function<TResult(TArgs...)>, TArgs...)> partialLambda; public: YBuilder(function<TResult(function<TResult(TArgs...)>, TArgs...)> _partialLambda) :partialLambda(_partialLambda) { } TResult operator()(TArgs ...args)const { return partialLambda( [this](TArgs ...args) { return this->operator()(args...); }, args...); } }; template<typename TMethod> struct PartialLambdaTypeRetriver { typedef void FunctionType; typedef void LambdaType; typedef void YBuilderType; }; template<typename TClass, typename TResult, typename ...TArgs> struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)> { typedef TResult FunctionType(TArgs...); typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...); typedef YBuilder<TResult, TArgs...> YBuilderType; }; template<typename TClass, typename TResult, typename ...TArgs> struct PartialLambdaTypeRetriver<TResult(__thiscall TClass::*)(function<TResult(TArgs...)>, TArgs...)const> { typedef TResult FunctionType(TArgs...); typedef TResult LambdaType(function<TResult(TArgs...)>, TArgs...); typedef YBuilder<TResult, TArgs...> YBuilderType; }; template<typename TLambda> function<typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::FunctionType> Y(TLambda partialLambda) { return typename PartialLambdaTypeRetriver<decltype(&TLambda::operator())>::YBuilderType(partialLambda); } int _tmain(int argc, _TCHAR* argv[]) { auto fib = Y([](function<int(int)> self, int index) { return index<2 ?1 :self(index-1)+self(index-2); }); for (int i = 0; i < 10; i++) { cout << fib(i) << " "; } cout << endl; }