]> https://gitweb.dealii.org/ - dealii.git/commitdiff
and port to C++14
authorMatthias Maier <tamiko@43-1.org>
Wed, 27 May 2020 02:01:29 +0000 (21:01 -0500)
committerMatthias Maier <tamiko@43-1.org>
Wed, 27 May 2020 16:26:14 +0000 (11:26 -0500)
include/deal.II/base/thread_local_storage.h

index c2a0976fa830c9cb8efe72366152b34ae03b2002..e9f1d778f5c8c7bea281290876057100974b6560 100644 (file)
@@ -263,6 +263,45 @@ namespace Threads
   {}
 
 
+#    ifndef DOXYGEN
+  namespace internal
+  {
+    /*
+     * We have to make sure not to call "data.emplace(id, *exemplar)" if
+     * the corresponding element is not copy constructible. We use some
+     * SFINAE magic to work around the fact that C++14 does not have
+     * "if constexpr".
+     */
+    template <typename T>
+    typename std::enable_if_t<
+      std::is_copy_constructible<typename unpack_vector<T>::type>::value,
+      T &>
+    construct_element(std::map<std::thread::id, T> &  data,
+                      const std::thread::id &         id,
+                      const std::shared_ptr<const T> &exemplar)
+    {
+      if (exemplar)
+        {
+          const auto it = data.emplace(id, *exemplar).first;
+          return it->second;
+        }
+      return data[id];
+    }
+
+    template <typename T>
+    typename std::enable_if_t<
+      !std::is_copy_constructible<typename unpack_vector<T>::type>::value,
+      T &>
+    construct_element(std::map<std::thread::id, T> &data,
+                      const std::thread::id &       id,
+                      const std::shared_ptr<const T> &)
+    {
+      return data[id];
+    }
+  } // namespace internal
+#    endif
+
+
   template <typename T>
   inline T &
   ThreadLocalStorage<T>::get(bool &exists)
@@ -298,20 +337,7 @@ namespace Threads
       // lock ensures that no other threat does a lookup at the same time.
       std::unique_lock<decltype(insertion_mutex)> lock(insertion_mutex);
 
-      if constexpr (std::is_copy_constructible<
-                      typename unpack_vector<T>::type>::value)
-        if (exemplar)
-          {
-            const auto it = data.emplace(my_id, *exemplar).first;
-            return it->second;
-          }
-
-      if constexpr (std::is_default_constructible<T>::value)
-        {
-          return data[my_id]; // invokes default constructor
-        }
-
-      Assert(false, ExcInternalError());
+      return internal::construct_element(data, my_id, exemplar);
     }
   }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.