「労働者が自分の仕事をうまくやりたいなら、まず自分の道具を研ぎ澄まさなければなりません。」 - 孔子、「論語。陸霊公」
表紙 > プログラミング > C ++ Lambdaのモバイルキャプチャの実装のヒント

C ++ Lambdaのモバイルキャプチャの実装のヒント

2025-04-12に投稿されました
ブラウズ:582

How to Implement Move Capture in C   Lambdas?

lambdas

question:

の移動キャプチャ)たとえば、

std::unique_ptr myPointer(new int);

std::function example = [std::move(myPointer)] {
   *myPointer = 4;
};

回答:

一般化されたラムダキャプチャC 14

で、一般化されたラムダキャプチャにより移動キャプチャが可能になります。このコードが有効になりました:
using namespace std;

auto u = make_unique(some, parameters);  
go.run([u = move(u)] { do_something_with(u); }); 
namespace std; auto u = make_unique (some、parameters); go.run([u = move(u)] {do_something_with(u);});

オブジェクトをラムダから別の関数に移動するには、ラムダを可変化可能にします:
using namespace std;

auto u = make_unique(some, parameters);  
go.run([u = move(u)] { do_something_with(u); }); 
go.run([u = move(u)] mutable {do_someething_with(std :: move(u));}) 11

ヘルパー関数、make_rrefは、移動キャプチャを促進できます。その実装は次のとおりです #include #include テンプレート 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 &&(){ std :: move(x)を返します。 } プライベート: t x; bool iscopied = false; }; テンプレート rref_impl make_rref(t && x){ REF_IMPL {std :: move(x)}を返します。 }

make_rrefのテストケース:

#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)};
}

エミュレート一般化されたラムダキャプチャのc #include int main(){ std :: unique_ptr p {new int(0)}; Auto Lambda = Capture(STD :: MOVE(P)、 [](std :: unique_ptr &p){return std :: move(p); }); assert(lambda()); assert(!lambda()); }
using namespace std;

auto u = make_unique(some, parameters);  
go.run([u = move(u)] { do_something_with(u); }); 
キャプチャは次のように実装されます:

#include テンプレート クラスCapture_Impl { t x; f f; 公共: Capture_impl(t && x、f && f) :x {std :: forward (x)}、f {std :: forward (f)} {} テンプレート auto operator()(ts && ... args) - > Decltype(f(x、std :: forward (args)...)){ futren f(x、std :: forward (args)...); } テンプレート auto operator()(ts && ... args)const - > Decltype(f(x、std :: forward (args)...)){ futren f(x、std :: forward (args)...); } }; テンプレート Capture_impl capture(t && x、f && f){ capture_impl を返します( std :: forward (x)、std :: forward (f)); }

このソリューションは、キャプチャされたタイプがコピー不可能な場合、ランバンのコピーを防ぎ、ランタイムエラーを回避します。

最新のチュートリアル もっと>

免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。

Copyright© 2022 湘ICP备2022001581号-3