title: How to write a function pointer to a function returning a function pointer to a function?
body: <p>I want to assign a function's address to a function pointer, but the function to be addressed returns a function pointer with the same signature as itself, causing it to recurse in a way that I can't write the return type at all, for the function pointer or even the function declaration itself...</p>
  
  <p>I guess a way of simplifying the problem so it's not confusing:</p>
  
  <p>How could I write a function declaration such, that it can return a pointer to itself (or any other function with the same signature)?</p>
  
  <pre><code>????? function(int a){
      // could be this function, or another with the same signature, 
      return arbitraryFunction;  
  }
  
  ?????(*func)(int) = function;  // same problem as above
  </code></pre>
  
  <p><strong>edit:</strong></p>
  
  <p>For now I have a solution, though I won't post it as an answer because it's aggressively ugly.  It gets rid of the recursion by simply returning a raw <code>void*</code> pointer as the return type, and ends up taking the following form:</p>
  
  <pre><code>void* function(int parameter){
      return arbitraryFunction; // of the same signature
  }
  
  void*(*func)(int) = function; 
  func = reinterpret_cast&lt;void*(*)(int)&gt;(func(42));  // sin
  </code></pre>
  
  <p><strong>edit2:</strong></p>
  
  <p>It seems casting between function pointers and regular pointers is UB, so I can't use <code>void*</code> in this case...</p>
  
  <p>To answer one of the comments, this is for passing control between multiple "main" loops in my program, with each loop getting it's own function.  There's a lot of ways to do this, but returning function pointers (or NULL to terminate the program) mid-loop <em>seemed</em> like the simplest method, but I didn't anticipate that pointers to data and pointers to function addresses would be incompatable with each other.  I think returning polymorphic function objects will end up being the more sane option in this case.</p>