jeudi 3 janvier 2019

what's wrong when I copy a lamda expression to std::function

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?

Aucun commentaire:

Enregistrer un commentaire