From 8ef2ae57c143bd27f4487c75fd8c9f6b5df2c61d Mon Sep 17 00:00:00 2001 From: Ole Erik Peistorpet Date: Thu, 6 Jun 2024 16:46:52 +0200 Subject: [PATCH] Fixed incorrect dynarray constructor sometimes chosen --- auxi/dynarray_detail.h | 9 ++++++++- dynarray.h | 3 ++- .../dynarray_construct_assignop_swap_gtest.cpp | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/auxi/dynarray_detail.h b/auxi/dynarray_detail.h index add5a7bc..770937ef 100644 --- a/auxi/dynarray_detail.h +++ b/auxi/dynarray_detail.h @@ -6,7 +6,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -#include "core_util.h" +#include "range_traits.h" // for iterator_t #include // for uintptr_t #include @@ -23,6 +23,13 @@ namespace oel::_detail } }; + + template< typename ToConstruct, typename FromRange, + typename /*EnableIfRange*/ = iterator_t, + typename SansCVRef = std::remove_cv_t< std::remove_reference_t > + > + using EnableIfShouldConstructFromRange = enable_if< !std::is_same_v >; + //////////////////////////////////////////////////////////////////////////////// struct DebugAllocationHeader diff --git a/dynarray.h b/dynarray.h index f8ba76ff..562a4cf2 100644 --- a/dynarray.h +++ b/dynarray.h @@ -94,7 +94,8 @@ class dynarray auto result = dynarray(boost::range::istream_range(someStream)); @endcode */ template< typename InputRange, - typename /*EnableIfRange*/ = iterator_t > + _detail::EnableIfShouldConstructFromRange = 0 + > explicit dynarray(InputRange && r, Alloc a = Alloc{}) : _m(a) { append(r); } dynarray(std::initializer_list il, Alloc a = Alloc{}) : _m(a) { append(il); } diff --git a/unit_test/dynarray_construct_assignop_swap_gtest.cpp b/unit_test/dynarray_construct_assignop_swap_gtest.cpp index b84ff886..974a2256 100644 --- a/unit_test/dynarray_construct_assignop_swap_gtest.cpp +++ b/unit_test/dynarray_construct_assignop_swap_gtest.cpp @@ -315,6 +315,21 @@ TEST_F(dynarrayConstructTest, constructMoveOnlyIterator) #endif +TEST_F(dynarrayConstructTest, copyConstruct) +{ + using Al = StatefulAllocator; + auto x = dynarray< TrivialRelocat, Al >({TrivialRelocat{0.5}}, Al(-5)); + EXPECT_EQ(1, g_allocCount.nAllocations); + + auto y = dynarray(x); + EXPECT_EQ(-5, y.get_allocator().id); + EXPECT_EQ(2, g_allocCount.nAllocations); + + auto z = dynarray(y, Al(7)); + EXPECT_EQ(0.5, *z.front()); + EXPECT_EQ(3, g_allocCount.nAllocations); +} + template void testMoveConstruct(Alloc a0, Alloc a1) {