我最近尝试做这样的事情:
auto x = std::make_unique<int>(1);
auto l = [y = std::move(x)]() { return *y; };
std::function<void()> f(std::move(l)); //error, requires copy construction
Run Code Online (Sandbox Code Playgroud)
令我非常失望和困惑的是,它向我抛出了一堆错误消息。如您所知,std::function
不允许从不可复制构造的类型进行构造。有什么具体原因吗?或者它是标准中的一个疏忽?仅移动类型的构造会带来什么问题?