Why doesn't the line `thread [I] = thread (exec_produce, I + 1) `report an error?

void exec_produce(int duration) {
    //duration
    this_thread::sleep_for(chrono::seconds(duration));
    //this_thread::get_id()id
    cout << "exec_produce thread " << this_thread::get_id()
        << " has sleeped " << duration << " seconds" << endl;
}

int main(int argc, const char *argv[])
{
    thread threads[5];
    cout << "create 5 threads ..." << endl;
    for (int i = 0; i < 5; iPP) {
        threads[i] = thread(exec_produce, i + 1);
    }
    cout << "finished creating 5 threads, and waiting for joining" << endl;
    //,copydelete
    /*for(auto it : threads) {
        it.join();
    }*/
    for (auto& it: threads) {
        it.join();
    }
    cout << "Finished!!!" << endl;
    system("pause");
    return 0;
}

I want to know why threads [I] = thread (exec_produce, I + 1); this line of code will not report an error, but here

    for(auto it : threads) {
        it.join();
    }

is it wrong? Aren"t assignment operators called in both places?

I know the following two rules:

1) move assignment operation: thread& operator= (thread&& rhs) noexcept, if the current object is not joinable, you need to pass a right value reference (rhs) to the move assignment operation; if the current object can be joinable, terminate () reports an error.

2) copy assignment is disabled: thread& operator= (const thread&) = delete,thread object cannot be copied.

there may be some doubts about move. Thank you ~

Feb.28,2021
Menu