From: Christopher Dykes Date: Sat, 4 Jun 2016 00:43:42 +0000 (-0700) Subject: Allow constexpr construction of AtomicStruct under MSVC X-Git-Tag: 2016.07.26~164 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=58d507c161c5e8b097796fe5fb05ccc73c564d5c;p=folly.git 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 --- 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();