使用std :: move发送std :: unique_ptr到VS1012中的std :: thread [英] Using std::move to send a std::unique_ptr to std::thread in VS1012
问题描述
以下是Visual Studio 2012中的错误。
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));
错误3错误C2248:'std :: unique_ptr< _Ty>: :unique_ptr':无法访问在'std :: unique_ptr< _Ty>'c:\program文件(x86)中声明的私有成员\microsoft visual studio 11.0 \vc\include\functional 1152 1 scratch
在功能上有助于指定这个定义:
_VARIADIC_EXPAND_0X(_CLASS_BIND,,,)
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, , , , )
是很好的:
do_something(std::move(i));
我做错了什么?
推荐答案
我做错了什么?
What am I doing wrong?
没有。事实上,你的程序是合法的,它的行为是明确定义的。
Almost nothing. In fact, your program is legal and its behavior is well defined.
你得到的编译器错误必然是在执行标准库编译器,或许与VC11不支持可变参数模板,并且用于伪造它们的基于宏的机器不完美的事实有关。
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.
这说,即使你的程序编译,你仍然必须加入你的线程或分离之前, std :: thread
RAII包装器被销毁 - 抛出一个异常,如果析构函数 std :: thread
在被封装的线程仍在运行时被调用(除非它被分离)。
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).
这篇关于使用std :: move发送std :: unique_ptr到VS1012中的std :: thread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!