#include <iostream>
#include <memory>
#include <future>
using namespace std;
unique_ptr<int> uq(new int);
void foo(unique_ptr<int> q)
{}
int main()
{
foo(move(uq));
// ^ OK
async(foo, move(uq));
// ^ Error: error C2248: 'std::unique_ptr<_Ty>::unique_ptr' :
// cannot access private member declared in class 'std::unique_ptr<_Ty>'
}
为什么“async”无法编译?我使用 Microsoft Visual Studio 2012 (v4.5.50709)。
这应该按照标准工作,并且它在 gcc 上工作.
在VS上失败的原因可能是因为std::async
允许存储其参数的内部副本,然后这些副本将在稍后传递给被调用的函数。
在这种情况下,就需要two移动到 unique_ptr:一个用于构造异步的中间对象,然后是第二个将参数传递给foo
。两者之一可能会失败。然而,该标准明确指出,论点async
只能是可移动构造的(§ 30.6.8.2),其中unique_ptr
is.
所以我想说这是VS2012标准库实现中的一个bug。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)