In connection with the question of how to describe the type of a function that takes another function as an argument , he remembered a question that Satter understood - how to declare the type of a function that accepts or returns itself (ie, a function of the same type). For those times there was no solution as such; there were some crutches and nothing more. Has the situation changed in C ++ 11 and C ++ 14? Is it possible to more or less honestly declare such a function?

    1 answer 1

    You can make a class with the operator (). It looks indistinguishable from the function (by the way, this is exactly how the lambda functions are implemented):

    #include "stdio.h" int k = 0; class MyFun{ public: auto operator () (){ k++; printf("Function called: %d\n",k); return *this; } }; main(){ MyFun fun; auto x = fun()()()()()(); } 

    Conclusion:

     Function called: 1 Function called: 2 Function called: 3 Function called: 4 Function called: 5 Function called: 6 
    • Yes. but, by the way, it could be done even then, with the old standard - just use MyFun& instead of auto instead. Although, of course, this is not really a function, but still ... In short, you need to sit down and think hard :), so for now - thanks! - Harry