From 6bdd3fde65654926332ec249c940eca275433de0 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sat, 22 Apr 2017 22:39:57 -0400 Subject: [PATCH] Add alternative sp_array_construct for trivially destructible case --- .../boost/smart_ptr/allocate_shared_array.hpp | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index b43b43d..c063a35 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -9,9 +9,10 @@ Distributed under the Boost Software License, Version 1.0. #define BOOST_SMART_PTR_ALLOCATE_SHARED_ARRAY_HPP #include -#include -#include #include +#include +#include +#include #include namespace boost { @@ -173,7 +174,7 @@ inline void sp_array_destroy(A& allocator, T* storage, std::size_t size) { while (size > 0) { - std::allocator_traits::destroy(allocator, &storage[--size]); + std::allocator_traits::destroy(allocator, storage + --size); } } #endif @@ -202,15 +203,27 @@ sp_array_construct(T* storage, std::size_t size) ::new(static_cast(storage + i)) T(); } } catch (...) { - while (i > 0) { - storage[--i].~T(); - } + sp_array_destroy(storage, i); throw; } } template -inline void +inline +typename sp_enable::value || + boost::has_trivial_destructor::value>::type +sp_array_construct(T* storage, std::size_t size, const T* list, + std::size_t count) +{ + for (std::size_t i = 0; i < size; ++i) { + ::new(static_cast(storage + i)) T(list[i % count]); + } +} + +template +inline +typename sp_enable::value && + !boost::has_trivial_destructor::value>::type sp_array_construct(T* storage, std::size_t size, const T* list, std::size_t count) { @@ -220,9 +233,7 @@ sp_array_construct(T* storage, std::size_t size, const T* list, ::new(static_cast(storage + i)) T(list[i % count]); } } catch (...) { - while (i > 0) { - storage[--i].~T(); - } + sp_array_destroy(storage, i); throw; } } @@ -332,9 +343,7 @@ sp_array_default(T* storage, std::size_t size) ::new(static_cast(storage + i)) T; } } catch (...) { - while (i > 0) { - storage[--i].~T(); - } + sp_array_destroy(storage, i); throw; } }