1

The sr.tFuture is initialized with 'empty' state instead of 'pending' and I found out when I remove tFuture() from the constructor initialization list, the state of sr.tFuture becomes pending which is correct. But I don't quite understand the reason. Is it because the value of tFuture on the declaration part gets overwritten when tFuture() gets invoked and cause tFuture to not get associated with any promise?

struct SharedResource
{
    SharedResource(const SharedResource&) = delete;
    SharedResource& operator=(const SharedResource&) = delete;
    SharedResource() :
        tPromise(), tFuture()
        {}

    std::promise<void> tPromise;
    std::future<void> tFuture = tPromise.get_future();
};

int main()
{
    SharedResource sr;
}
Sami
  • 513
  • 4
  • 11
  • related/dupe: https://stackoverflow.com/questions/53396645/are-there-2-times-initialization-when-there-is-a-constructor-with-default-argume – NathanOliver Mar 29 '23 at 17:51
  • 3
    `= tPromise.get_future();` is a default initializer. That is, the initializer to use when a constructor doesn't provide one. When you add `tFuture()` to your initializer list, the default initializer is ignored because you provide your own initializer instead. – François Andrieux Mar 29 '23 at 17:53
  • @FrançoisAndrieux if 'tFuture()' is ignore, how come removing it make the sr.tFuture in 'pending' state which is correct and not removing it , make the 'sr.tFuture' in empty state which is not correct! – Sami Mar 29 '23 at 17:58
  • 2
    @Sam You misunderstood my comment. It isn't `tFuture()` that is ignored if you add it. It is the default initializer `= tPromise.get_future();` that is ignored, because it is only used when no other initializer is provided. – François Andrieux Mar 29 '23 at 18:25
  • I am so sorry! Now that makes sense a lot and a lot ! I had thought 'tPromise.get_future();' would be overwritten by 'tFuture()', which I was wrong. Thanks a lot. Vey helpful! – Sami Mar 29 '23 at 18:27
  • @FrançoisAndrieux something just came to my mind: The default member initializer tPromise.get_future() is indeed specified at the point of member declaration. However, it doesn't get executed immediately at the point of declaration. It is only executed during the object construction if no explicit initializer is provided in the constructor's member initializer list. am I right? – Sami Mar 29 '23 at 19:54
  • @Sam , in absence of a `SharedResource` instance, how can an instance member `tFuture` be constructed? Automatic storage demands the scope of an object be allocated, before the object is constructed. – Red.Wave Mar 29 '23 at 20:27
  • 1
    @Sam It sounds like you understand correctly, mostly. A default initializer is just an initializer that the compiler uses as the default when one isn't provided for a constructor. If it is ever executed, it is just before the constructor body, when members are initialized. When you remove `tFuture()` from the initializer list, behind the curtains it actually becomes `tPromise(), tFuture{ tPromise.get_future() }`. The only time it might makes sense to consider a default member initializer running "at the point of declaration" is for `static` members, and even then it is nuanced. – François Andrieux Mar 29 '23 at 21:26
  • Thank you. That behind the curtain was interesting to know! Appreciate the explanations! – Sami Mar 29 '23 at 22:35

0 Answers0