I have a function that builds a lambda function with motion capture (C ++ 1y only) and returns it.
#include <iostream> #include <functional> #include <memory> using namespace std; function<int ()> makeLambda(unique_ptr<int> ptr) { return [ ptr( move(ptr) ) ] () { return *ptr; }; } int main() { // Works { unique_ptr<int> ptr( new int(10) ); auto function1 = [ ptr(move(ptr)) ] { return *ptr; }; } // Does not work { unique_ptr<int> ptr( new int(10) ); auto function2 = makeLambda( std::move(ptr) ); } return 0; }
However, it appears that the copy constructor is called upon return unique_ptr<int>. Why is this / how can I get around this?
unique_ptr<int>
Link for insertion: http://coliru.stacked-crooked.com/a/b21c358db10c3933
std::function<int ()> return, . , - std::unique_ptr. , std::function, , .
std::function<int ()>
std::unique_ptr
std::function
auto makeLambda(unique_ptr<int> ptr) { return [ ptr( move(ptr) ) ] () { return *ptr; }; }
, makeLambda unique_ptr<int>&&
makeLambda
unique_ptr<int>&&
Source: https://habr.com/ru/post/1545769/More articles:Where is the annotation in sleep mode with the date as a condition - javaПредложение, показывающее половину в конце div, переполнение: скрытое сокращает предложение - htmlКак заставить InvariantCulture распознавать запятую как десятичный разделитель? - floating-pointR - Delete a tag without deleting a label - rSystem.Convert.ToSingle(), (1.5) vs (1,5) - stringQML - Passing an associative Javascript array in C ++ - c ++Are you running test run tests at the same time? - unit-testinghttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1545772/spring-repository-annotation-does-not-work-but-service-annotation-does&usg=ALkJrhj_5huIA7iMaXOiQ8kLdkPEv7JsvAHow to RelativeLayout Align Center android - androidWhen to delete GDI objects, in particular, bitmaps - c #All Articles