dimanche 20 juin 2021

Why can't std::function accept a movable but non-copyable lambda object?

#include <functional>

int main()
{
    struct A
    {
        A() = default;
        
        A(A&&)     = default;
        A& operator=(A&&) = default;

        A(A const&&) = delete;
        A& operator=(A const&) = delete;
    };

    auto a = A{};
    auto f = [a = std::move(a)] {}; // ok

    std::function<void()> fn3 = [a = std::move(a)] {}; // error
}

Both of gcc and clang reject the code. See online demo

Why can't std::function accept a movable but non-copyable lambda object?

Aucun commentaire:

Enregistrer un commentaire