引用文章 如何在lambda中引入递归调用

// clang++ 3.5
// maybe gcc 4.9 support it, but I don't test it
#include<iostream>
int main()
{
auto fac = [&](auto&& self, int x)->int{
return x < ? : x * self(self, x - );
};
std::cout<<fac(fac, )<<std::endl; //
return ;
} 作者:蓝色
链接:https://www.zhihu.com/question/27441424/answer/36643770
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
// clang++ 3.5
// maybe gcc 4.9 support it, but I don't test it
#include<iostream>
template<typename Functor>
struct wrapper_type
{
Functor functor;
template<typename... Args>
decltype(auto) operator()(Args&&... args) const&
{
return functor(functor, std::forward<Args>(args)...);
}
}; template<typename Functor>
wrapper_type<typename std::decay<Functor>::type> wrapper(Functor&& functor)
{
return{ std::forward<Functor>(functor) };
} int main()
{
auto fac = wrapper([&](auto&& self, int x)->int{
return x < ? : x * self(self, x - );
});
std::cout << fac() << std::endl; //
return ;
}
作者:蓝色
链接:https://www.zhihu.com/question/27441424/answer/36643770
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
 
#include <iostream>
#include <typeinfo>
#include <functional>
using namespace std;
int main(void)
{
std::function<int(int)> product;
product = [&product](int n) -> int{ return n <= ? : n * product(n - ); };
cout << "The answer is: " << product() << endl;
} 链接:https://blog.csdn.net/zenny_chen/article/details/6045596
上一篇:jvm到底是什么?有什么作用?工作机制如何?


下一篇:vivado中如何使用chipscope