From 8956d48f8e77b54d50166dbfc43027e64a2f9657 Mon Sep 17 00:00:00 2001 From: Matthias Maier Date: Tue, 26 May 2020 21:01:29 -0500 Subject: [PATCH] and port to C++14 --- include/deal.II/base/thread_local_storage.h | 54 +++++++++++++++------ 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/include/deal.II/base/thread_local_storage.h b/include/deal.II/base/thread_local_storage.h index c2a0976fa8..e9f1d778f5 100644 --- a/include/deal.II/base/thread_local_storage.h +++ b/include/deal.II/base/thread_local_storage.h @@ -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 std::enable_if_t< + std::is_copy_constructible::type>::value, + T &> + construct_element(std::map & data, + const std::thread::id & id, + const std::shared_ptr &exemplar) + { + if (exemplar) + { + const auto it = data.emplace(id, *exemplar).first; + return it->second; + } + return data[id]; + } + + template + typename std::enable_if_t< + !std::is_copy_constructible::type>::value, + T &> + construct_element(std::map &data, + const std::thread::id & id, + const std::shared_ptr &) + { + return data[id]; + } + } // namespace internal +# endif + + template inline T & ThreadLocalStorage::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 lock(insertion_mutex); - if constexpr (std::is_copy_constructible< - typename unpack_vector::type>::value) - if (exemplar) - { - const auto it = data.emplace(my_id, *exemplar).first; - return it->second; - } - - if constexpr (std::is_default_constructible::value) - { - return data[my_id]; // invokes default constructor - } - - Assert(false, ExcInternalError()); + return internal::construct_element(data, my_id, exemplar); } } -- 2.39.5