Another strange question. Brought to you by Discovering Modern C ++ Gottschlig. I am trying to write a template that returns a functor that uses a certain function recursively predetermined number of times. Like that:
template<typename Func, typename X, int N> class Recurs { public: Recurs(Func f):f(f){} X operator()(X x) { return f(Recurs<Func,X,N-1>(f)(x)); } private: Func f; }; template<typename Func, typename X> class Recurs<Func,X,1> { public: Recurs(Func f):f(f){} X operator()(X x) { return f(x); } private: Func f; }; Since the use in the spirit
auto f = [](double x) { return cos(x); }; Recurs<decltype(f),double,10> r(f); r(0.1); does not warm the soul, did this function:
template<int N, typename X, typename Func> auto calc_N(Func f, X x) -> decltype(f(x)) { return Recurs<Func,X,N>(f)(x); } The function itself derives types from the values passed, so that it can be calculated as
calc_N<10>(f,0.1); But I didn’t like the Recurs... object being created every time Recurs... I would like to receive it once, and call it as a functor - like,
template<int N, typename Func> decltype(auto) make_N(Func f) ... auto g = make_N<10>(f); g(0.1); And here I have a plug. I wrote something like this -
template<int N, typename Func> decltype(auto) make_N(Func f) { return Recurs<Func,???,N>(f); } but what should I write instead of ??? . In principle, in this case, both the type of the argument f and the type of its return value will suit me (they must be the same for recursion), but how to write them?
And yet - as I recall, decltype(auto) is already C ++ 14. Is it possible to create such a thing on younger versions of the language, and how?