C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::is_lambda

From: Christoph Meyer <christoph.meyer.2006_at_[hidden]>
Date: Wed, 22 Feb 2023 14:10:51 +0100
On Wednesday, 22 February 2023 10:09:31 MET, Frederick Virchanza Gotham via Std-Proposals
wrote:
> Why don't we have a means of determining if something is a lambda?
>
> template<typename Lambda>
> requires std::is_lambda_v<Lambda>
> struct OnScopeExit {
> Lambda f;
> OnScopeExit(Lambda arg) : f(arg) {}
> ~OnScopeExit(void) { f(); }
> };
>
> void Func(void) { }
>
> int main(void)
> {
> OnScopeExit myguard([](){ /* Do Something */ });
> }

[using markdown]
Three things:

1. What are the benefits? Please elaborate.

2. What still counts as a lambda? What about lambdas forwarded from one function to another? What about this:
```c++
void f1(void (*lambda)());
void f2(void (*&lambda)());

int main()
{
    auto my_lambda = [](){ /* ... */ };
    f1(my_lambda); // Is this considered a lambda?
    f2(my_lambda); // Or this?
    
    auto my_lambda_copy = my_lambda;
    auto& my_lambda_ref = my_lambda;
    // How about these two lines?
    f1(my_lambda_copy);
    f1(my_lambda_ref);
    
    return 0;
}
```

3. Let's assume every function pointer that originated as a lambda still counts as a lambda. Imagine you're working with pointer arithmetic. In that case -- and in some other cases, too -- it is impossible to determine if a function is a lambda or not at compile-time and therefore requires some kind of run-time type checking.

Sincerely,
Christoph Meyer

Received on 2023-02-22 13:11:04