shared_future::get postcondition

32 views
Skip to first unread message

Sergey Vidyuk

unread,
Jan 2, 2017, 9:59:25 AM1/2/17
to ISO C++ Standard - Discussion
C++ 14 standard specifies std::future::get() postcondition: valid() == false in the paragraph 30.6.6 and do not specifies any postcondition for std::shared_future::get() in the paragraph 30.6.7. There is a note for the generic version of the shared_future::get:

Note: Access through that reference after the shared state has been destroyed produces undefined behavior; this can be avoided by not storing the reference in any storage with a greater lifetime than the shared_future object that returned the reference.

This note looks like indirect requirement to keep reference to a shared_state after get is called. Is it correct to assume that shared_future must be valid after get() is called? What is the reason behind the fact that there is no validity postcondition specified for the shared_future::get?

Sergey Vidyuk

Anthony Williams

unread,
Jan 3, 2017, 5:33:57 AM1/3/17
to std-dis...@isocpp.org
shared_future::get returns a reference to the object stored in the
shared state. shared_future::get() can be called multiple times and will
return the same reference every time.

Since it is a reference, not a value, it is possible for the reference
to outlive the shared state (e.g. by destroying all the futures
immediately after storing the reference in a local variable).

The note just makes it clear that such a dangling reference is undefined
behaviour (as dangling references always are), so don't do that.

Anthony
--
Author of C++ Concurrency in Action http://www.stdthread.co.uk/book/
just::thread C++11 thread library http://www.stdthread.co.uk
Just Software Solutions Ltd http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976
Reply all
Reply to author
Forward
0 new messages