Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::move to send a std::unique_ptr to std::thread in VS1012

The following gives me an error in visual studio 2012.

void do_something(std::unique_ptr<int> i);
std::unique_ptr<int> i(new int);
std::thread(do_something, std::move(i));

Error 3 error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>' c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 1 scratch It is helpfully(!) pointing at this definition in functional: _VARIADIC_EXPAND_0X(_CLASS_BIND, , , , )

This is fine:

do_something(std::move(i));

What am I doing wrong?

like image 628
doctorlove Avatar asked May 23 '13 10:05

doctorlove


1 Answers

What am I doing wrong?

Almost nothing. In fact, your program is legal and its behavior is well defined.

The compiler error you are getting is necessarily a bug in the implementation of the Standard Library that ships with your compiler, perhaps connected with the fact that VC11 does not support variadic templates, and the macro-based machinery used to fake them is not perfect.

This said, even if your program did compile, you would still have to join your thread or detach from it before the std::thread RAII wrapper gets destroyed - an exception is thrown if the destructor of std::thread is invoked while the encapsulated thread is still running (unless it was detached).

like image 169
Andy Prowl Avatar answered Nov 08 '22 13:11

Andy Prowl