#include <functional>
#include <memory>
#include <string>
#include <iostream>
void foo(std::function<void()> f)
{
f();
}
int main()
{
std::unique_ptr<int> handle = std::make_unique<int>(5);
foo(
[h = std::move(handle)]() mutable
{
std::cout << *h << std::endl;
});
}
Following code doen't compile with error
C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29910\include\functional(914,58): error C2280: 'main::<lambda_1aa523ba59bbecea5306bc1fd178120f>::<lambda_1aa523ba59bbecea5306bc1fd178120f>(const main::<lambda_1aa523ba59bbecea5306bc1fd178120f> &)': attempting to reference a deleted function
- Why it is happen?
- Is there are way how pass std::unique_ptr in std::function<void()> without passing it by reference?