为什么无法将捕获 unique_ptr 的 lamda 推送到 的容器中std::function
?这是正确的标准行为还是 VC 问题?有没有办法解决这个问题?
#include <memory>
#include <functional>
#include <vector>
using namespace std;
int main()
{
unique_ptr<int> up{ new int{} };
vector<function<void()>> cont;
cont.push_back([up_{ move(up) }]() {}); // Error here
return 0;
}
VC 编译器错误(Visual Studio v15.8.9):
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1195): error C2280: 'main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>::<lambda_4c17d669ccc0e2d463a7085245c40bfc>(const main::<lambda_4c17d669ccc0e2d463a7085245c40bfc> &)': attempting to reference a deleted function
non-cpy-lambda.cpp(11): note: see declaration of 'main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>::<lambda_4c17d669ccc0e2d463a7085245c40bfc>'
non-cpy-lambda.cpp(11): note: 'main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>::<lambda_4c17d669ccc0e2d463a7085245c40bfc>(const main::<lambda_4c17d669ccc0e2d463a7085245c40bfc> &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)'
with
[
_Ty=int
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\memory(2337): note: 'std::unique_ptr<int,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': function was explicitly deleted
with
[
_Ty=int
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1213): note: see reference to function template instantiation 'std::_Func_impl_no_alloc<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,_Ret>::_Func_impl_no_alloc<const _Callable&,void>(_Other)' being compiled
with
[
_Ret=void,
_Callable=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,
_Other=const main::<lambda_4c17d669ccc0e2d463a7085245c40bfc> &
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1214): note: see reference to function template instantiation 'std::_Func_impl_no_alloc<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,_Ret>::_Func_impl_no_alloc<const _Callable&,void>(_Other)' being compiled
with
[
_Ret=void,
_Callable=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,
_Other=const main::<lambda_4c17d669ccc0e2d463a7085245c40bfc> &
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1213): note: while compiling class template member function 'std::_Func_base<_Ret> *std::_Func_impl_no_alloc<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,_Ret>::_Clone(void *,std::false_type) const'
with
[
_Ret=void
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1204): note: see reference to function template instantiation 'std::_Func_base<_Ret> *std::_Func_impl_no_alloc<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,_Ret>::_Clone(void *,std::false_type) const' being compiled
with
[
_Ret=void
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1061): note: see reference to class template instantiation 'std::_Func_impl_no_alloc<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,_Ret>' being compiled
with
[
_Ret=void
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1313): note: see reference to class template instantiation 'std::_Is_large<_Impl>' being compiled
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1502): note: see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>>(_Fx &&)' being compiled
with
[
_Ret=void,
_Fx=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>
]
c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.15.26726\include\functional(1502): note: see reference to function template instantiation 'void std::_Func_class<_Ret>::_Reset<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>>(_Fx &&)' being compiled
with
[
_Ret=void,
_Fx=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>
]
non-cpy-lambda.cpp(11): note: see reference to function template instantiation 'std::function<void (void)>::function<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,void>(_Fx)' being compiled
with
[
_Fx=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>
]
non-cpy-lambda.cpp(11): note: see reference to function template instantiation 'std::function<void (void)>::function<main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>,void>(_Fx)' being compiled
with
[
_Fx=main::<lambda_4c17d669ccc0e2d463a7085245c40bfc>
]