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

心不动则不痛 提交于 2019-12-19 13:09:08

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/16712038/using-stdmove-to-send-a-stdunique-ptr-to-stdthread-in-vs1012

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!