Home > Backend Development > C++ > How Can I Correctly Capture Unique Pointers in C Lambda Expressions?

How Can I Correctly Capture Unique Pointers in C Lambda Expressions?

Patricia Arquette
Release: 2024-11-21 05:23:10
Original
587 people have browsed it

How Can I Correctly Capture Unique Pointers in C   Lambda Expressions?

Capturing Unique Pointers in Lambda Expressions

It is common to encounter difficulties when attempting to capture unique pointers within lambda expressions. Consider the following scenario:

std::function<void()> getAction(std::unique_ptr<MyClass> &&psomething) {
    // Caller gives ownership of psomething
    return [psomething]() {
        psomething->do_some_thing();
        // psomething is expected to be released after this point
    };
}
Copy after login

The above code fails to compile due to the inability of the lambda to capture the unique pointer by reference. To resolve this issue, C 14 introduced lambda generalized capture.

Lambda Generalized Capture in C 14

Lambda generalized capture allows for capturing variables by value or by move. This is achieved by utilizing the = move() syntax when capturing the unique pointer, as seen below:

std::function<void()> getAction(std::unique_ptr<MyClass> psomething) {
    // Caller gives ownership of psomething
    return [auto psomething = std::move(psomething)]() {
        psomething->do_some_thing();
        // psomething is expected to be released after this point
    };
}
Copy after login

By using auto, the lambda automatically deduces the type of psomething, which is a unique pointer. The = std::move(psomething) expression explicitly moves ownership of the unique pointer into the lambda expression.

Implementation of Move and Copy

The implementation of the copy and move functions provided in the question is valid for two-phase copy/move (as described in [this StackOverflow question](https://stackoverflow.com/questions/6322951/whats-the-difference-between-a-copy-constructor-and-a-move-constructor)).

However, it is important to note that C 11 introduced a different meaning for the move function through the std::move expression. The std::move expression rvalue-qualifies the object it is applied to, essentially indicating that it should be moved. Therefore, in the context of lambda generalized capture, it is not necessary to explicitly call std::move on the unique pointer; the lambda will automatically move the ownership as needed.

The above is the detailed content of How Can I Correctly Capture Unique Pointers in C Lambda Expressions?. 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