Skip to content

Commit

Permalink
Fixed incorrect dynarray constructor sometimes chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
OleErikPeistorpet committed Jun 6, 2024
1 parent 0bd4b90 commit 8ef2ae5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
9 changes: 8 additions & 1 deletion auxi/dynarray_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdint> // for uintptr_t
#include <stdexcept>
Expand All @@ -23,6 +23,13 @@ namespace oel::_detail
}
};


template< typename ToConstruct, typename FromRange,
typename /*EnableIfRange*/ = iterator_t<FromRange>,
typename SansCVRef = std::remove_cv_t< std::remove_reference_t<FromRange> >
>
using EnableIfShouldConstructFromRange = enable_if< !std::is_same_v<SansCVRef, ToConstruct> >;

////////////////////////////////////////////////////////////////////////////////

struct DebugAllocationHeader
Expand Down
3 changes: 2 additions & 1 deletion dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class dynarray
auto result = dynarray(boost::range::istream_range<int>(someStream));
@endcode */
template< typename InputRange,
typename /*EnableIfRange*/ = iterator_t<InputRange> >
_detail::EnableIfShouldConstructFromRange<dynarray, InputRange> = 0
>
explicit dynarray(InputRange && r, Alloc a = Alloc{}) : _m(a) { append(r); }

dynarray(std::initializer_list<T> il, Alloc a = Alloc{}) : _m(a) { append(il); }
Expand Down
15 changes: 15 additions & 0 deletions unit_test/dynarray_construct_assignop_swap_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ TEST_F(dynarrayConstructTest, constructMoveOnlyIterator)
#endif


TEST_F(dynarrayConstructTest, copyConstruct)
{
using Al = StatefulAllocator<TrivialRelocat>;
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<typename Alloc>
void testMoveConstruct(Alloc a0, Alloc a1)
{
Expand Down

0 comments on commit 8ef2ae5

Please sign in to comment.