Date: Thu, 15 May 2025 23:59:17 +0100
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.
>
> 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.
Received on 2025-05-15 22:59:29