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

Why Can't I Create a `std::function` from a Move-Capturing Lambda?

Susan Sarandon
Release: 2024-12-17 01:22:25
Original
274 people have browsed it

Why Can't I Create a `std::function` from a Move-Capturing Lambda?

Creating std::function from Move-Capturing Lambda Expression

Understanding the issue of creating std::function objects from lambda expressions is crucial. It is possible to create lambda expressions with move capture, but when attempting to encapsulate them within std::function, errors may arise.

Error Examples

Multiple attempts to convert a move-capturing lambda to std::function consistently lead to the error "Call to implicitly-deleted copy constructor of '

The Need for Move-Capturing Lambdas

Move capture in lambda expressions becomes necessary in scenarios where sharing ownership of an object is not viable, such as in custom UI libraries. These libraries offer methods like on_mouse_down() and push_undo_action() to register event handlers using std::function. To ensure efficient ownership of resources, move capture in lambda expressions is preferred over the earlier, cumbersome "release/acquire-in-lambda" idiom.

Restrictions on std::function Construction

The std::function constructor for directly specifying a function object has the following signature:

template <class F> function(F f);</p>
<p>However, another related constructor accepts an allocator and a reference to a type that provides certain guarantees:</p>
<pre class="brush:php;toolbar:false">template <class F, class A> function(allocator_arg_t, const A& a, F f);
Copy after login

These guarantees include the following:

  • F must be copy constructible
  • f must be callable with the provided argument types (ArgTypes) and return type (R)
  • The copy constructor and destructor of A must not throw exceptions

Applicability to Move-Capture

While lambda expressions with move capture do not violate the copyability requirement of F, they often move-capture types (such as std::unique_ptr) that do not provide the required copy constructor. As a result, the construction of std::function from these move-capturing lambdas becomes infeasible.

Conclusion

It is important to note that the conversion of move-capturing lambdas to std::function is not possible due to the limitations of the std::function constructor in handling move-only types. This restriction stems from the necessity of std::function to maintain a copy of the encapsulated function object.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template