Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why stl vector can't contain coroutine objects?

I use coroutine in boost1.53, see my code below:

boost::coroutines::coroutine<int()> f(std::bind(foo, ...));
std::vector<decltype(f)> container; // it can be compiled
container.push_back(f); // compile error

the error:

no matching function for call to ‘std::vector<boost::coroutines::coroutine<int(),0> >::vector(paracel::coroutine<int>&)’

Update: The error is occured because there are no copy construction/operator in 'boost::coroutines::coroutine', case here is I only want to save the 'f's into a container which map a index to 'f'.

I also tried unordered_map, and emplace_back, it still can not work!

How can I make it work in C++?

Update2: I tried vector,unordered_map, map together with emplace_back, push_back, std::move and all failed. But list and deque is ok with push_back/emplace_back and 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

Why?

like image 875
xunzhang Avatar asked Apr 27 '26 09:04

xunzhang


1 Answers

It looks as if boost::coroutines::coroutines<int()> doesn't support a copy constructor. You try to push_back() an lvalue, however. You might want to try moving the object into vector, though:

container.push_back(std::move(f));
like image 89
Dietmar Kühl Avatar answered Apr 28 '26 22:04

Dietmar Kühl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!