+ 1

(Why) is there no recursion pointer in C++?

In C++ every object has a pointer called "this" which points to the object itself. 1.) Is there also a pointer pointing to the function in which it is called? 2.) If not, why? Advantages: If you change the function name you wouldn't have to change every recursive function call inside that function. That would also help you to avoid errors. Disadvantage: could be less readable? 3.) Do other languages have this feature? Example (I called the pointer to the function it ist called in "recursion"): // you could e.g. write this: void func(int i) { recursion(i); } // instead of this: void func(int i) { func(i); }

5th Dec 2019, 6:24 PM
Paul
Paul - avatar
5 Respostas
+ 2
I actually like the idea. It's not useful for what you described, because you can just use the name of the function, but some functions don't have names: [](int n) -> int { // this is an anonymous function. // How can I possibly recurse here? } Javascript actually had a "recursion pointer", called `arguments.callee`. It is deprecated... but because of obscure javascript problems and not because it's a bad idea per se. I'm all for it even though the use cases are pretty limited. And as far as programming theory goes, you can implement a "recursion operator" that will pass the function you apply it to as it's first parameter, in pretty much any programming language. It's called the Y combinator and it is my profile status, check it out :) You'd use it like this (pseudocode, assuming `Y` is correctly implemented): Y(fn (recurse, n) { if n == 0: return; recurse(n-1); })(10);
5th Dec 2019, 8:51 PM
Schindlabua
Schindlabua - avatar
0
Yes, you are right. It's much more convenient to write auto func = [](int i){ recursion(i); }; instead of std::function<void(int)> func = [&func](int i){ func(i); };
5th Dec 2019, 9:09 PM
Paul
Paul - avatar
0
thats a good solution but you still have the problem that you have to store the function pointer in a variable. I think Y combinators are a better technique to solve the problem
5th Dec 2019, 10:24 PM
Paul
Paul - avatar