1

the code will be compiled with eror

class A
{

};

int main()
{
    auto a = std::make_unique<A>();
    std::function<void()> fun = [ap = std::move(a)](){

    };
}

but it's ok after i user auto instead of std::function

class A
{

};

int main()
{
    auto a = std::make_unique<A>();
    auto fun = [ap = std::move(a)](){

    };
}

the error is like this:

C:/Qt/Qt5.11.1/Tools/mingw530_32/i686-w64-mingw32/include/c++/functional:1710:34: error: use of deleted function 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)'
    __dest._M_access<_Functor*>() =
                                  ^
C:\Users\Xiaopeng\CLionProjects\testGP\main.cpp:98:51: note: 'main()::<lambda()>::<lambda>(const main()::<lambda()>&)' is implicitly deleted because the default definition would be ill-formed:
     std::function<void()> fun = [ap = std::move(a)](){
                                                   ^
C:\Users\Xiaopeng\CLionProjects\testGP\main.cpp:98:51: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = A; _Dp = std::default_delete<A>]'
In file included from C:/Qt/Qt5.11.1/Tools/mingw530_32/i686-w64-mingw32/include/c++/memory:81:0,
                 from C:\Users\Xiaopeng\CLionProjects\testGP\main.cpp:3:
C:/Qt/Qt5.11.1/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/unique_ptr.h:356:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
   ^

what's wrong with my code when user std::function?

T.C.
  • 133,968
  • 17
  • 288
  • 421
peng
  • 11
  • 1

1 Answers1

2

std::function requires the callable stored within it to be copyable. Your lambda is not copyable, because it contains move-only data member.

The second version works because it does not create a std::function object, it makes fun be of the lambda's closure type (which is the type of the lambda expression itself).

If you need to store the lambda in a std::function (e.g. because you need to store it in a data member), you'll have to somehow make it copyable. In your case, the simplest solution is to use a std::shared_ptr instead of a std::unique_ptr.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • thanks for your patient answer. Another question is that if I want to store the lambda to a class member variable, how can do this? std::move seems not ok. – peng Jan 04 '19 at 08:32