2016-12-16 4 views
0

構造体MyParamを作成し、任意の数のパラメータ(この場合はintおよびbool)を使用してインスタンス化できるようにしました。カプセル化の理由から、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)) 
関連する問題