I have a function that returns a reference to a std::promise:
std::shared_ptr<std::promise<void>> play();
(More info: The function plays media on some device, and the return value represents when this playing is complete. If play is called a second time, a value is set on the promise returned the first time, and a new promise is created and returned for this second call)
The caller can then catch the value and wait on the future:
auto this_future = play()->get_future();
this_future.wait();
Does it make sense to return a reference to the promise, or should I return the future instead, so that the calling function does not have to call get_future()?
std::movefutures if you want to store then in a collection, saystd::vector<std::future<T>>. - NawazplayAll()to returnstd::vector<std::future<void>>instead of the corresponding collection of promise refereneces. - Scott Mstd::futuremeans that only ONE consumer should ever have it and think they have the right to.get()from it. With astd::shared_ptr<std::future<X>>, you are saying "more than one client has equal ownership of thisfuture". This is code smell. Similarly,std::promise<X>is supposed to be uniquely owned by the producer of the data: having two pieces of code with shared ownership implies both have the right to set its value (or read its future).play()should returnstd::future<void>. Internally astd::promise<void> old_promisecan be kept around. - Yakk - Adam Nevraumont