我在 boost1.53 中使用协程,请参见下面的代码:
boost::coroutines::coroutine<int()> f(std::bind(foo, ...));
std::vector<decltype(f)> container; // it can be compiled
container.push_back(f); // compile error
错误:
no matching function for call to ‘std::vector<boost::coroutines::coroutine<int(),0> >::vector(paracel::coroutine<int>&)’
更新:发生错误是因为“boost::coroutines::coroutine”中没有复制构造/运算符,这里的情况是我只想将“f”保存到将索引映射到“f”的容器中。
我也试过unordered_map和emplace_back,还是不行!
我怎样才能使它在 C++ 中工作?
更新2:我尝试了vector、unordered_map、map以及emplace_back、push_back、std::move,但都失败了。但是列表和双端队列可以使用 push_back/emplace_back 和 std::move:
std::deque<decltype(f)> container1;
container.push_back(std::move(f)); // ok
std::deque<decltype(f)> container2;
container.emplace_back(std::move(f)); // ok
std::list<decltype(f)> container3;
container.push_back(std::move(f)); // ok
std::list<decltype(f)> container4;
container.emplace_back(std::move(f)); // ok
为什么?