From: Matthias Maier Date: Wed, 27 May 2020 02:01:29 +0000 (-0500) Subject: and port to C++14 X-Git-Tag: v9.3.0-rc1~1535^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8956d48f8e77b54d50166dbfc43027e64a2f9657;p=dealii.git and port to C++14 --- 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); } }