Home > Backend Development > C++ > How to Implement Move Capture in C Lambdas?

How to Implement Move Capture in C Lambdas?

Linda Hamilton
Release: 2024-12-26 13:35:10
Original
784 people have browsed it

How to Implement Move Capture in C   Lambdas?

Move Capture in Lambdas

Question:

How do we implement move capture, also known as rvalue references, in C 11 lambdas? For instance:

std::unique_ptr<int> myPointer(new int);

std::function<void(void)> example = [std::move(myPointer)] {
   *myPointer = 4;
};
Copy after login

Answer:

Generalized Lambda Capture in C 14

In C 14, generalized lambda capture allows for move capture. This code is now valid:

using namespace std;

auto u = make_unique<some_type>(some, parameters);  
go.run([u = move(u)] { do_something_with(u); }); 
Copy after login

To move objects from a lambda to another function, make the lambda mutable:

go.run([u = move(u)] mutable { do_something_with(std::move(u)); });
Copy after login

Workaround for Move Capture in C 11

A helper function, make_rref, can facilitate move capture. Its implementation is as follows:

#include <cassert>
#include <memory>
#include <utility>

template <typename T>
struct rref_impl {
    rref_impl() = delete;
    rref_impl(T&& x) : x{std::move(x)} {}
    rref_impl(rref_impl& other)
        : x{std::move(other.x)}, isCopied{true}
    {
        assert(other.isCopied == false);
    }
    rref_impl(rref_impl&& other)
        : x{std::move(other.x)}, isCopied{std::move(other.isCopied)}
    {
    }
    rref_impl& operator=(rref_impl other) = delete;
    T& operator&&() {
        return std::move(x);
    }

private:
    T x;
    bool isCopied = false;
};

template<typename T> rref_impl<T> make_rref(T&& x) {
    return rref_impl<T>{std::move(x)};
}
Copy after login

A test case for make_rref:

int main() {
    std::unique_ptr<int> p{new int(0)};
    auto rref = make_rref(std::move(p));
    auto lambda =
        [rref]() mutable -> std::unique_ptr<int> { return rref.move(); };
    assert(lambda());
    assert(!lambda());
}
Copy after login

Emulating Generalized Lambda Capture in C 11

Another workaround is provided by the capture() function:

#include <cassert>
#include <memory>

int main() {
    std::unique_ptr<int> p{new int(0)};
    auto lambda = capture(std::move(p),
        [](std::unique_ptr<int>& p) { return std::move(p); });
    assert(lambda());
    assert(!lambda());
}
Copy after login

capture is implemented as follows:

#include <utility>

template <typename T, typename F>
class capture_impl {
    T x;
    F f;
public:
    capture_impl(T&& x, F&& f)
        : x{std::forward<T>(x)}, f{std::forward<F>(f)} {}

    template <typename ...Ts> auto operator()(Ts&& ...args)
        -> decltype(f(x, std::forward<Ts>(args)...)) {
        return f(x, std::forward<Ts>(args)...);
    }

    template <typename ...Ts> auto operator()(Ts&& ...args) const
        -> decltype(f(x, std::forward<Ts>(args)...)) {
        return f(x, std::forward<Ts>(args)...);
    }
};

template <typename T, typename F>
capture_impl<T, F> capture(T&& x, F&& f) {
    return capture_impl<T, F>(
        std::forward<T>(x), std::forward<F>(f));
}
Copy after login

This solution prevents copying the lambda if the captured type is not copyable, avoiding runtime errors.

The above is the detailed content of How to Implement Move Capture in C Lambdas?. 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