Reputation: 536
I wrote the struct MyParam
so that it could instantiate using any number of parameters, which in this case are an int
and a bool
. For encapsulation reasons, I would like for MyParams
to contain its own promise
so it can report when it is done doing something. But when I add the statement to the struct, it fails. As a global, though, it works fine. Here is the code:
#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
What am I missing with regards to the promise
statement as a member?
Upvotes: 0
Views: 687
Reputation: 109159
std::promise
is not copy-constructible.
std::make_unique< MyParams< int, bool > >( myParams )
Above, make_unique
is attempting to copy construct a MyParams< int, bool >
which is ill-formed due to the presence of the promise
data member. You can get the code to compile if you move construct instead.
std::make_unique< MyParams< int, bool > >( std::move(myParams) )
Upvotes: 2