On Thu, May 15, 2025 at 2:16 PM Pavel Vazharov wrote:
>
> C++23 is enough to cover you here:
>
> #include <print>
>
> int main(int const argc, char **const argv)
> {
> auto func = [argv](this auto&& self, int count)
> {
> if ( 0 == count ) return;
> std::println("{}", argv[count - 1]);
> self(count - 1);
> };
> func(argc);
> }
int main(int const argc, char **const argv)
{
auto func1 = +[](int count, char **argv)
{
// This one cannot recurse
};
auto func2 = +[](this auto &&self, int count, char **argv)
{
// This one won't compile
};
}
Normally if a lambda doesn't have any captures, you can turn it into a
normal function pointer by using the unary '+' operator. However in
your code example, the lambda cannot be turned into a normal function
pointer -- because it needs to be invoked on an object (i.e. it needs
the 'this' pointer).
So we still don't have a 'perfect' solution here yet, not even with
C++23. I think for perfection we'd need std::recurse.
--
Std-Proposals mailing list
Std-Proposals@lists.isocpp.org
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals