test_once
Summary: [Folly] `test_once`, a way to check whether any call to `call_once` with a given `once_flag` has succeeded. One example of use might be for exception-safety guarding object destruction when object construction is guarded by the `once_flag`, and when the user is interested in conserving per-object memory and wishes to avoid the extra 8-byte overhead of `std::optional`. ```lang=c++ template <typename T> struct Lazy { folly::aligned_storage_for_t<T> storage; folly::once_flag once; ~Lazy() { if (folly::test_once(once)) { reinterpret_cast<T&>(storage).~T(); } } template <typename... A> T& construct_or_fetch(A&&... a) { folly::call_once(once, [&] { new (&storage) T(std::forward<A>(a)...); }); return reinterpret_cast<T&>(storage); } }; ``` Reviewed By: ovoietsa Differential Revision: D13561365 fbshipit-source-id: 8376c154002f1546f099903c4dc6be94dd2def8e
Showing
Please register or sign in to comment