Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need a fixed point combinator. See <a href="https://stackoverflow.com/questions/6453011/my-implementation-of-fixed-point-combinator-in-c0x-compiled-under-vistual-stud">this</a>.</p> <p>or look at the following code:</p> <pre><code>//As decltype(variable)::member_name is invalid currently, //the following template is a workaround. //Usage: t2t&lt;decltype(variable)&gt;::t::member_name template&lt;typename T&gt; struct t2t { typedef T t; }; template&lt;typename R, typename V&gt; struct fixpoint { typedef std::function&lt;R (V)&gt; func_t; typedef std::function&lt;func_t (func_t)&gt; tfunc_t; typedef std::function&lt;func_t (tfunc_t)&gt; yfunc_t; class loopfunc_t { public: func_t operator()(loopfunc_t v)const { return func(v); } template&lt;typename L&gt; loopfunc_t(const L &amp;l):func(l){} typedef V Parameter_t; private: std::function&lt;func_t (loopfunc_t)&gt; func; }; static yfunc_t fix; }; template&lt;typename R, typename V&gt; typename fixpoint&lt;R, V&gt;::yfunc_t fixpoint&lt;R, V&gt;::fix = [](fixpoint&lt;R, V&gt;::tfunc_t f) -&gt; fixpoint&lt;R, V&gt;::func_t { fixpoint&lt;R, V&gt;::loopfunc_t l = [f](fixpoint&lt;R, V&gt;::loopfunc_t x) -&gt; fixpoint&lt;R, V&gt;::func_t{ //f cannot be captured since it is not a local variable //of this scope. We need a new reference to it. auto &amp;ff = f; //We need struct t2t because template parameter //V is not accessable in this level. return [ff, x](t2t&lt;decltype(x)&gt;::t::Parameter_t v){ return ff(x(x))(v); }; }; return l(l); }; int _tmain(int argc, _TCHAR* argv[]) { int v = 0; std::function&lt;int (int)&gt; fac = fixpoint&lt;int, int&gt;::fix([](std::function&lt;int (int)&gt; f) -&gt; std::function&lt;int (int)&gt;{ return [f](int i) -&gt; int{ if(i==0) return 1; else return i * f(i-1); }; }); int i = fac(10); std::cout &lt;&lt; i; //3628800 return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload