Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does std::forward not work in the lambda body?

#include <utility>

void f(auto const& fn1)
{
    {
        auto fn2 = std::forward<decltype(fn1)>(fn1);
        auto fn3 = std::forward<decltype(fn2)>(fn2); // ok
        fn3();
    }

    [fn2 = std::forward<decltype(fn1)>(fn1)]
    {
        auto const fn3 = fn2;
        auto fn4 = std::forward<decltype(fn3)>(fn3); // ok
        fn4();
    
        auto fn5 = std::forward<decltype(fn2)>(fn2); // error
        fn5();
    }();
}

int main()
{
    f([] {});
}

godbolt demo

Why does std::forward not work in the lambda body?


Updated Information:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

g++ is ok, but clang++ rejects it. Who is correct?

>Solution :

Clang is correct to reject it.

decltype(fn2) gives the type of fn2, suppose the lambda closure type is T, then it’ll be T. Function-call operator of the lambda is const-qualified, then std::forward<decltype(fn2)>(fn2) fails to be called. The template argument for std::forward is specified as T explicitly, then std::forward<decltype(fn2)> is supposed to accept T& (and T&&) as its parameter type, but a const fn2 can’t be bound to reference to non-const.

As the workaround you might mark the lambda as mutable.

[fn2 = std::forward<decltype(fn1)>(fn1)] mutable
{
    auto fn3 = std::forward<decltype(fn2)>(fn2); // fine
    fn3();
}();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading