给定 std::futures
的 std::vector
std::vector<std::future<void>> futures;
为什么 std::fill_n
提示我在传递右值时调用复制构造函数:
std::fill_n(std::back_inserter(futures), 10, std::async([]{ std::cout << "yo\n"; }));
error: use of deleted function future(const future&)
我的意思是,fill_n
不会收到一个 xvalue 吗?如果没有,为什么不呢?
最佳答案
std::fill_n
将给定对象的拷贝作为第三个参数传递,但第三个参数是不可复制的 future 对象,因为复制 future 没有意义。因此错误。
看来你需要这个:
std::generate_n(std::back_inserter(futures),
10,
[]{ return std::async([]{ std::cout << "yo\n"; }); });
关于c++ - std::fill_n 的右值,是否只支持复制填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022229/