Recursive typedef function definition : std::function returning its own type
c++, c++11
Solution
Since recursive type definition is not possible, you can declare a structure that carry the function and implicitly cast to it:
template< typename... T >
struct RecursiveHelper
{
typedef std::function< RecursiveHelper(T...) > type;
RecursiveHelper( type f ) : func(f) {}
operator type () { return func; }
type func;
};
typedef RecursiveHelper<int&>::type callback_t;
Example: http://coliru.stacked-crooked.com/a/c6d6c29f1718e121
Problem
I am trying to implement a state-machine. The state is represented by a function of type `callback_t` : `callback_t(int&)` which returns a function of same type. I dont know how to implement it since recursive typed function seems not to be allowed. Here what I tryied (as a toy) : ``` #include <stdio.h> #include <functional> typedef std::function< callback_t(int &) > callback_t ; callback_t f1(int & i) { i++; return f1; } callback_t f0(int & i) { if(i==0) i++; return f1; } callback_t start(int & i) { i=0; return f0; } int main(int argc, char **argv) { callback_t begin = start; int i=0; while(i<100) begin = begin(i); printf("hello world\n"); return 0; } ``` The error: ``` C:/work/tests/tests/main.cpp:4:41: error: 'callback_t' was not declared in this scope typedef std::function< callback_t(int &) > callback_t ; ^ ``` Is there a way to implement this kind of behaviour ? Env : win7, codelite, mingw 4.8.1