Capture and move a unique_ptr in a c++14 lambda expression

后端 未结 3 941
夕颜
夕颜 2021-02-01 01:44

I am capturing a unique_ptr in a lambda expression this way:

auto str = make_unique(\"my string\");
auto lambda = [ capturedStr = std::move(str)          


        
3条回答
  •  广开言路
    2021-02-01 02:24

    To make the advice more explicit: add mutable: http://coliru.stacked-crooked.com/a/a19897451b82cbbb

    #include 
    
    int main()
    {
        std::unique_ptr pi(new int(42));
    
        auto ll = [ capturedInt = std::move(pi) ] () mutable { };
    }
    

提交回复
热议问题