Recursive typedef function definition : std::function returning its own type

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

Leave a Comment