From af8fb707dab1fab5cf444632fb25823b628e62f5 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Thu, 8 Jul 2021 19:53:58 +0200 Subject: BLI: avoid calling deleted copy constructor in some compilers Previously, this did not compile in VS 2017, because `new T(initializer_())` would try to call the copy constructor of `T`. Now, `initializer_` will construct the `T` inplace. --- source/blender/blenlib/BLI_enumerable_thread_specific.hh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_enumerable_thread_specific.hh b/source/blender/blenlib/BLI_enumerable_thread_specific.hh index 25fd02b41fb..3051d980d45 100644 --- a/source/blender/blenlib/BLI_enumerable_thread_specific.hh +++ b/source/blender/blenlib/BLI_enumerable_thread_specific.hh @@ -77,17 +77,18 @@ template class EnumerableThreadSpecific : NonCopyable, NonMovable { * their addresses do not change when the map grows. */ Map> values_; Vector> owned_values_; - std::function initializer_; + std::function initializer_; public: using iterator = typename Map>::MutableValueIterator; - EnumerableThreadSpecific() : initializer_([]() { return T(); }) + EnumerableThreadSpecific() : initializer_([](void *buffer) { new (buffer) T(); }) { } template - EnumerableThreadSpecific(F initializer) : initializer_(std::move(initializer)) + EnumerableThreadSpecific(F initializer) + : initializer_([=](void *buffer) { new (buffer) T(initializer()); }) { } @@ -96,11 +97,10 @@ template class EnumerableThreadSpecific : NonCopyable, NonMovable { const int thread_id = enumerable_thread_specific_utils::thread_id; std::lock_guard lock{mutex_}; return values_.lookup_or_add_cb(thread_id, [&]() { - /* `std::make_unique` does not work here if T is non-copyable and non-movable. */ - std::unique_ptr value{new T(initializer_())}; - std::reference_wrapper ref = *value; - owned_values_.append(std::move(value)); - return ref; + T *value = (T *)::operator new(sizeof(T)); + initializer_(value); + owned_values_.append(std::unique_ptr{value}); + return std::reference_wrapper{*value}; }); } -- cgit v1.2.3