Allow constexpr construction of AtomicStruct under MSVC
authorChristopher Dykes <cdykes@fb.com>
Sat, 4 Jun 2016 00:43:42 +0000 (17:43 -0700)
committerFacebook Github Bot 4 <facebook-github-bot-4-bot@fb.com>
Sat, 4 Jun 2016 00:53:51 +0000 (17:53 -0700)
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

index 0587b964060b16c3c1db3df6aa49c318ad66331e..2fc6d6e42cb2e6357c1925e9ae95e3c746b5261e 100644 (file)
@@ -43,7 +43,10 @@ class AtomicStruct {
                 folly::IsTriviallyCopyable<T>::value,
       "target type must be trivially copyable");
 
-  Atom<Raw> data;
+  union {
+    Atom<Raw> 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<T> const &) = delete;
   AtomicStruct<T>& operator= (AtomicStruct<T> 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();