Std :: move operation C ++

Lines from Anthony Williams book:

The following example uses std :: move to transfer ownership of a dynamic object to a stream:

void process_big_object(std::unique_ptr<big_object>);

std::unique_ptr<big_object> p(new big_object);
p->prepare_data(42);
std::thread t(process_big_object,std::move(p));

By specifying std::move(p)in the constructor std::thread, ownership is big_objecttransferred first to the internal storage for the newly created stream, and then to process_big_object.

I understand the stack and the bunch; any idea that this is actually internal storage ?

Why can't they directly transfer ownership of process_big_object?

+6
source share
3 answers

, std::thread, .

, std::thread. - . , " ".

+11

, std::thread, .

std::thread, .

+9

process_big_object?

Since there is no line in the code fragment where it process_big_objectis called as a function. The last line of the fragment calls the constructor std::thread. It will set in motion a chain of events that will ultimately cause a process_big_object(p)call in a new thread; but this call is not displayed here.

0
source

All Articles