简体   繁体   English

Visual Studio中的std :: packaged_task错误?

[英]std::packaged_task bug in Visual Studio?

I found out that a std::packaged_task couldn't be pushed into a std::vector if the parameter type returns void in Visual Studio (2012, 2013, Nov 2013 CTP). 我发现,如果参数类型在Visual Studio中返回void ,则无法将std::packaged_task推送到std::vector中(2012、2013、2013年11月CTP)。 For example, 例如,

typedef std::packaged_task<void()> packaged_task;

std::vector<packaged_task> tasks;
packaged_task package;
tasks.push_back(std::move(package));

The error messages are: 错误消息是:

error C2182: '_Get_value' : illegal use of type 'void'
error C2182: '_Val' : illegal use of type 'void'
error C2182: '_Val' : illegal use of type 'void'
error C2512: 'std::_Promise<int>' : no appropriate default constructor available
error C2665: 'std::forward' : none of the 2 overloads could convert all the argument types

I think this is bug because this code snippet works if 我认为这是错误,因为如果

  1. the return type is not void , 返回类型不是void
  2. it is compiled in XCode. 它是用XCode编译的。

Are there solutions or other options in Visual Studio? Visual Studio中是否有解决方案或其他选项? I know that boost can be used to replace this. 我知道可以使用boost来代替它。

I can reproduce this with a simple auto m = std::move(package); 我可以用一个简单的auto m = std::move(package);复制auto m = std::move(package); .

int main()
{
    typedef std::packaged_task<void()> packagedtask;
    packagedtask p1;
    packagedtask p2;
    p2 = std::move(p1); // does not cause the error
    auto p3 = std::move(p2); // causes the error
}

Trawling through the code, packaged_task has embedded typedefs as follows; 遍历代码, packaged_task嵌入了如下的typedef。

typedef typename _P_arg_type<_Ret>::type _Ptype;
typedef _Promise<_Ptype> _MyPromiseType;

_P_arg_type offers a non-void type when the return type is void . 当返回类型为void时, _P_arg_type提供一个非空类型。 The packaged_task move constructor contains a reference to the internal _Promise as _Promise<_Ret> ; packaged_task move构造函数包含对内部_Promise的引用,为_Promise<_Ret>

_MyPromise(_STD forward<_Promise<_Ret> >(_Other._MyPromise))

This then becomes _Promise<void> which in turn generates further invalid code that generates the list of errors seen. 然后,它变为_Promise<void> ,进而生成进一步的无效代码,该代码生成看到的错误列表。 It should probably be; 可能应该是;

_MyPromise(_STD forward<_MyPromiseType >(_Other._MyPromise))
// possibly even using a move

As the move assignment operator does. 与移动分配运算符一样。

As a workaround, consider adding a "dummy" or "unusable" return type of some sort; 解决方法是,考虑添加某种“虚拟”或“不可用”的返回类型。

struct unusable {};

Or just simply an int or boost as you have already suggested. 或者只是您已经建议的int或boost。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM