C++ Logo

std-discussion

Advanced search

Reassignment and broken promises

From: Bjorn Reese <breese_at_[hidden]>
Date: Wed, 28 Jul 2021 16:31:19 +0200
The move constructor and move assignment of std::promise in does not
mention the effects on associated futures when a promise is reassigned.

Consider the following code:

     #include <future>

     int main()
     {
         std::promise<int> promise;
         auto result = promise.get_future();
         promise = decltype(promise){}; // Reassigned
         promise.set_value(1);
         return result.get();
     }

In this case I would expect the future to be notified about a broken
promise, just as if the promise had been destroyed.

The code above gives different results for the main standard library
implementations.

   * libc++ throws a broken promise exception from future::get().
   * libstdc++ throws an accidental and unknown exception from
     promise::set_value().
   * MSVC returns 0.

The behaviour can be made consistent by changing [future.promise] from

     promise(promise&& rhs) noexcept;
         Effects: Transfers ownership of the shared state of rhs (if any)
         to the newly-constructed object.

into

     promise(promise&& rhs) noexcept;
         Effects: Abanons any shared state (32.9.5) and then transfers
         ownership of the shared state of rhs (if any) to the newly-
         constructed object.

Should I report this as an LWG defect?

Received on 2021-07-28 09:31:27