Date: Fri, 16 May 2025 18:54:40 -0500
> If we were to have a standard library function called "std::recurse",
then we could rewrite the above function as:
Even if this were possible, I don't see how this is better.
> This might make things a little more convenient when making a minimal
revision to an SVN or Github repo (i.e. you only have to edit one line
if you change the name of the function).
If you change the name of a function you already have to change it
everywhere it's called. A recursive call isn't special or worth
special-casing.
> It would also be more
convenient for code generation tools in that they don't have to keep
track of the name of the function
It's not hard at all to keep track of the name of a function. "keep track"
is really overstating how much work it is.
> There isn't any way to pull this off without using "std::function".
Yes it is, you can use deducing this.
> However, if C++29 were to have "std::recurse", then we could do:
And how would such a thing be implemented? It can't be without magic.
Jeremy
On Thu, May 15, 2025 at 8:10 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> Here's a recursive function:
>
> unsigned Factorial(unsigned const n)
> {
> if ( 1u == n ) return 1u;
> return n * Factorial(n - 1u);
> }
>
> If we were to have a standard library function called "std::recurse",
> then we could rewrite the above function as:
>
> unsigned Factorial(unsigned const n)
> {
> if ( 1u == n ) return 1u;
> return n * std::recurse(n - 1u);
> }
>
> This might make things a little more convenient when making a minimal
> revision to an SVN or Github repo (i.e. you only have to edit one line
> if you change the name of the function). It would also be more
> convenient for code generation tools in that they don't have to keep
> track of the name of the function -- but of course these two benefits
> are no big deal.
>
> Here is how people are currently writing recursive lambdas:
>
> #include <iostream>
> #include <functional>
>
> int main(int const argc, char **const argv)
> {
> std::function<void(int)> func =
> [&func,argv](int const count)
> {
> if ( 0 == count ) return;
> std::cout << argv[count - 1] << std::endl;
> func(count - 1);
> };
>
> func(argc);
> }
>
> There isn't any way to pull this off without using "std::function".
> However, if C++29 were to have "std::recurse", then we could do:
>
> #include <iostream>
>
> int main(int const argc, char **const argv)
> {
> auto func = [argv](int const count)
> {
> if ( 0 == count ) return;
> std::cout << argv[count - 1] << std::endl;
> std::recurse(count - 1);
> };
>
> func(argc);
> }
>
> Perhaps, in the case of nested lambdas, we could even specify a number
> to std::recurse to specify which function to re-enter. Looking at the
> above code snippet:
>
> std::recurse<0> = Re-enter the immediate function (might be a lambda)
> std::recurse<-1> = Re-enter the enclosing function (might be a lambda)
> std::recurse<-2> = Re-enter the enclosing enclosing function (and so
> on...)
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
then we could rewrite the above function as:
Even if this were possible, I don't see how this is better.
> This might make things a little more convenient when making a minimal
revision to an SVN or Github repo (i.e. you only have to edit one line
if you change the name of the function).
If you change the name of a function you already have to change it
everywhere it's called. A recursive call isn't special or worth
special-casing.
> It would also be more
convenient for code generation tools in that they don't have to keep
track of the name of the function
It's not hard at all to keep track of the name of a function. "keep track"
is really overstating how much work it is.
> There isn't any way to pull this off without using "std::function".
Yes it is, you can use deducing this.
> However, if C++29 were to have "std::recurse", then we could do:
And how would such a thing be implemented? It can't be without magic.
Jeremy
On Thu, May 15, 2025 at 8:10 AM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
> Here's a recursive function:
>
> unsigned Factorial(unsigned const n)
> {
> if ( 1u == n ) return 1u;
> return n * Factorial(n - 1u);
> }
>
> If we were to have a standard library function called "std::recurse",
> then we could rewrite the above function as:
>
> unsigned Factorial(unsigned const n)
> {
> if ( 1u == n ) return 1u;
> return n * std::recurse(n - 1u);
> }
>
> This might make things a little more convenient when making a minimal
> revision to an SVN or Github repo (i.e. you only have to edit one line
> if you change the name of the function). It would also be more
> convenient for code generation tools in that they don't have to keep
> track of the name of the function -- but of course these two benefits
> are no big deal.
>
> Here is how people are currently writing recursive lambdas:
>
> #include <iostream>
> #include <functional>
>
> int main(int const argc, char **const argv)
> {
> std::function<void(int)> func =
> [&func,argv](int const count)
> {
> if ( 0 == count ) return;
> std::cout << argv[count - 1] << std::endl;
> func(count - 1);
> };
>
> func(argc);
> }
>
> There isn't any way to pull this off without using "std::function".
> However, if C++29 were to have "std::recurse", then we could do:
>
> #include <iostream>
>
> int main(int const argc, char **const argv)
> {
> auto func = [argv](int const count)
> {
> if ( 0 == count ) return;
> std::cout << argv[count - 1] << std::endl;
> std::recurse(count - 1);
> };
>
> func(argc);
> }
>
> Perhaps, in the case of nested lambdas, we could even specify a number
> to std::recurse to specify which function to re-enter. Looking at the
> above code snippet:
>
> std::recurse<0> = Re-enter the immediate function (might be a lambda)
> std::recurse<-1> = Re-enter the enclosing function (might be a lambda)
> std::recurse<-2> = Re-enter the enclosing enclosing function (and so
> on...)
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2025-05-16 23:54:54