Home >Backend Development >C++ >Why Can't I Create an `std::function` from a Move-Capturing Lambda with a Non-Copyable Object?

Why Can't I Create an `std::function` from a Move-Capturing Lambda with a Non-Copyable Object?

Linda Hamilton
Linda HamiltonOriginal
2024-12-18 13:25:24908browse

Why Can't I Create an `std::function` from a Move-Capturing Lambda with a Non-Copyable Object?

Creating an std::function from a Move-Capturing Lambda Expression: Unveiling the Limitations

In the realm of C , the task of constructing an std::function from a move-capturing lambda expression often arises. While move-capturing lambda expressions themselves can be created seamlessly, the enigma lies in the inability to encapsulate them within an std::function.

As our example illustrates:

auto pi = std::make_unique<int>(0);
auto foo = [q = std::move(pi)] {
    *q = 5;
    std::cout << *q << std::endl;
};

This example effortlessly constructs a move-capturing lambda expression without encountering any obstacles. However, when attempting to embrace this lambda within an std::function, we encounter a barrage of errors:

std::function<void()> bar = foo;
std::function<void()> bar{foo};
std::function<void()> bar{std::move(foo)};
std::function<void()> bar = std::move(foo);
std::function<void()> bar{std::forward<std::function<void()>>(foo)};
std::function<void()> bar = std::forward<std::function<void()>>(foo);

Delving into the intricacies of the std::function class, we discover the following constructor:

template<class F> function(F f);

This constructor unveils the underlying issue: it necessitates that the encapsulated function F be copy-constructible. However, our move-capturing lambda expression does not meet this criterion; instead, it move-captures a non-copy-constructible object.

Therefore, we conclude that while constructing an std::function from a move-capturing lambda expression is feasible, it faces an insurmountable hurdle when the lambda move-captures a non-copy-constructible object. In such scenarios, alternative approaches, such as the "release/acquire-in-lambda" idiom, become necessary.

The above is the detailed content of Why Can't I Create an `std::function` from a Move-Capturing Lambda with a Non-Copyable Object?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn