From 58d507c161c5e8b097796fe5fb05ccc73c564d5c Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Fri, 3 Jun 2016 17:43:42 -0700 Subject: [PATCH] Allow constexpr construction of AtomicStruct under MSVC Summary: Attempting to use this under MSVC, as `MemoryIdler::defaultIdleTimeout` does, generated warnings about the symbol being dynamically initialized due to an implementation limation. Solve this by defining a union instead of calling `encode` in the constructor. Reviewed By: yfeldblum Differential Revision: D3387916 fbshipit-source-id: b0d628b6d086960e94121b6183a31348fb151aac --- folly/AtomicStruct.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/folly/AtomicStruct.h b/folly/AtomicStruct.h index 0587b964..2fc6d6e4 100644 --- a/folly/AtomicStruct.h +++ b/folly/AtomicStruct.h @@ -43,7 +43,10 @@ class AtomicStruct { folly::IsTriviallyCopyable::value, "target type must be trivially copyable"); - Atom data; + union { + Atom data; + T typedData; + }; static Raw encode(T v) noexcept { // we expect the compiler to optimize away the memcpy, but without @@ -65,7 +68,7 @@ class AtomicStruct { AtomicStruct(AtomicStruct const &) = delete; AtomicStruct& operator= (AtomicStruct const &) = delete; - constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {} + constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {} bool is_lock_free() const noexcept { return data.is_lock_free(); -- 2.34.1