The error I get says that the fill_n line below is trying to use the deleted copy constructor: why is it not trying to use the move constructor? I tried wrapping it in a std::move but that didn’t help.
std::vector< std::thread > workers;
workers.reserve( 10 );
std::fill_n( std::back_inserter( workers ), 10, std::thread( []{ std::cout << "thread\n"; } ) );
However, if I change the fill_n line to
for( int i = 0; i < 10; ++i )
{
workers.push_back( std::thread( []{ std::cout << "thread\n"; } ) );
}
that works fine. I thought these were essentially the same as I’ve done changes from the one to the other before in somewhat similar looking code.
On the line
there is a single temporary
std::threadinstance created, andfill_ntries to make 10 copies of it to fillworkers. You cannot move an object to make many copies – it just doesn’t make any sense.What you want is probably
std::generate_n: