2016-12-16 76 views
0

我写了结构MyParam,以便它可以使用任意数量的参数进行实例化,在本例中为intbool。出于封装原因,我希望MyParams包含自己的promise,以便它可以报告何时完成某件事情。但是当我将这个语句添加到结构中时,它失败了。尽管如此,作为一个全球性的公司,它运作良好下面是代码:当包含一个std :: promise作为成员时,为什么make_unique结构失败?

#include <tuple> 
#include <memory> 
#include <future> 

//std::promise<bool> donePromise; // OK: if at global level, then make_unique error goes away 

template< typename... ArgsT > 
struct MyParams 
{ 
    MyParams(ArgsT... args) : rvalRefs { std::forward_as_tuple(args...) } {} 

    std::tuple<ArgsT...> rvalRefs; 
    std::promise<bool> donePromise; // causes make_unique to fail when donePromise is a member of MyParams 
}; 

int main() 
{ 
    int num { 33 }; 
    bool tf { false }; 
    MyParams< int, bool > myParams(num, tf); // OK 
    auto myParamsUniquePtr = std::make_unique< MyParams< int, bool > >(myParams); 

    std::future<bool> doneFuture = myParams.donePromise.get_future(); // OK: when donePromise is a global 

    return 0; 
} 
error C2280: 'MyParams<int,bool>::MyParams(const MyParams<int,bool> &)': attempting to reference a deleted function 

什么我与问候到promise语句作为成员失踪?

回答

2

std::promise不可复制构建。

std::make_unique< MyParams< int, bool > >(myParams) 

以上,make_unique试图复制其构造是形成不良的,由于promise数据成员的存在的MyParams< int, bool >。如果您移动构造,则可以获得编译代码。

std::make_unique< MyParams< int, bool > >(std::move(myParams)) 
相关问题