C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Compiler support to use std::apply with template functions

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 23 May 2024 23:14:54 +0100
On Thu, May 23, 2024 at 5:49 PM Lorand Szollosi wrote:
>
> #define LIFT(F) [&](auto&&… args) { return F(std:::forward<decltype(args)>…); }
>
> And then, you can call std::apply(LIFT(Func), std::move(t));


That's really cool, thanks, I'm going to use it in my own code. But
I'll change the lambda's return type as follows:

    #define LIFT(F) [&](auto&&… args) -> decltype(auto) { return
F(std:::forward<decltype(args)>…); }

Here's an example of a scenario in which you need 'decltype(auto)'
instead of just plain 'auto':

    int &Func1(void)
    {
        static int i = 0;
        return i;
    }

    void Func2(int &arg)
    {
        arg = 1;
    }

    int main(void)
    {
        auto mylambda = []() -> decltype(auto) { return Func1(); };

        Func2( mylambda() );
    }

Received on 2024-05-23 22:15:07