Mover Capture en lambdas
pregunta:
¿Cómo implementamos Mover Capture, también conocido como referencias de RValue, en c 11 lambdas? Por ejemplo:
std::unique_ptrmyPointer(new int); std::function example = [std::move(myPointer)] { *myPointer = 4; };
Respuesta:
Generalized Lambda Capture en C 14
en C 14, captura de lambda generalizada permite la captura de movimiento. Este código ahora es válido:
using namespace std; auto u = make_unique(some, parameters); go.run([u = move(u)] { do_something_with(u); });
para mover objetos de una lambda a otra función, haga que la lambda mutable:
go.run([u = move(u)] mutable { do_something_with(std::move(u)); });[&]
Workaround para mude Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in Capture in capture in capture in ctain 11
una función de ayuda, make_rref, puede facilitar la captura de movimiento. Su implementación es la siguiente:#incluir
#includeun caso de prueba para make_rref:#include #include template 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 rref_impl make_rref(T&& x) { return rref_impl {std::move(x)}; }
int main () { std :: unique_ptr
#include#include #include template 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 rref_impl make_rref(T&& x) { return rref_impl {std::move(x)}; }
emulando la captura de lambda generalizada en c 11
Otra vigilancia es proporcionada por la función capture ():#include
#includecaptura se implementa de la siguiente manera:#include #include template 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 rref_impl make_rref(T&& x) { return rref_impl {std::move(x)}; }
#include
#includeEsta solución evita copiar el lambda si el tipo capturado no es copiado, evitando los errores de tiempo de ejecución.#include #include template 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 rref_impl make_rref(T&& x) { return rref_impl {std::move(x)}; }
Descargo de responsabilidad: Todos los recursos proporcionados provienen en parte de Internet. Si existe alguna infracción de sus derechos de autor u otros derechos e intereses, explique los motivos detallados y proporcione pruebas de los derechos de autor o derechos e intereses y luego envíelos al correo electrónico: [email protected]. Lo manejaremos por usted lo antes posible.
Copyright© 2022 湘ICP备2022001581号-3