Skip to content

Commit

Permalink
explicit dynarray copy constructors
Browse files Browse the repository at this point in the history
Also operator =(const dynarray &&) = delete
  • Loading branch information
OleErikPeistorpet committed Jun 6, 2024
1 parent 7a366ce commit f43bb5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
28 changes: 22 additions & 6 deletions dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,30 @@ class dynarray
auto result = dynarray(boost::range::istream_range<int>(someStream));
@endcode */
template< typename InputRange,
typename /*EnableIfRange*/ = iterator_t<InputRange>,
enable_if< !_detail::isSameSansCVRef<InputRange, dynarray> > = 0
typename /*EnableIfRange*/ = iterator_t<InputRange>
>
explicit dynarray(InputRange && r, Alloc a = Alloc{}) : _m(a) { append(r); }

dynarray(std::initializer_list<T> il, Alloc a = Alloc{}) : _m(a) { append(il); }

dynarray(dynarray && other) noexcept : _m(std::move(other._m)) {}
dynarray(dynarray && other) noexcept : _m(std::move(other._m)) {}
dynarray(dynarray && other, Alloc a);
dynarray(const dynarray & other) : dynarray(other,
_alloTrait::select_on_container_copy_construction(other._m)) {}
dynarray(const dynarray & other, Alloc a) : _m(a) { append(other); }

explicit dynarray(const dynarray & other) : dynarray(other,
_alloTrait::select_on_container_copy_construction(other._m)) {}
explicit dynarray(dynarray & other) : dynarray(static_cast<const dynarray &>(other)) {}
explicit dynarray(const dynarray && other) : dynarray(other) {}
explicit dynarray(const dynarray & other, Alloc a) : _m(a) { append(other); }
explicit dynarray(dynarray & other, Alloc a) : dynarray(static_cast<const dynarray &>(other), std::move(a)) {}
explicit dynarray(const dynarray && other, Alloc a) : dynarray(other, std::move(a)) {}

~dynarray() noexcept;

dynarray & operator =(dynarray && other) &
noexcept(_alloTrait::propagate_on_container_move_assignment::value or _alloTrait::is_always_equal::value);
//! Requires that allocator_type is always equal or does not have propagate_on_container_copy_assignment
dynarray & operator =(const dynarray & other) &;
dynarray & operator =(const dynarray &&) = delete;

dynarray & operator =(std::initializer_list<T> il) & { assign(il); return *this; }

Expand Down Expand Up @@ -924,6 +929,17 @@ explicit dynarray(InputRange &&, Alloc = {})
Alloc
>;

#if defined __GNUC__ and __GNUC__ < 12
template< typename T, typename A >
explicit dynarray(const dynarray<T, A> &) -> dynarray<T, A>;

template< typename T, typename A >
explicit dynarray(dynarray<T, A> &) -> dynarray<T, A>;

template< typename T, typename A >
explicit dynarray(const dynarray<T, A> &&) -> dynarray<T, A>;
#endif

#if OEL_MEM_BOUND_DEBUG_LVL
} // namespace debug
#endif
Expand Down
12 changes: 8 additions & 4 deletions unit_test/dynarray_construct_assignop_swap_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,20 +314,24 @@ 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));
auto const z = dynarray(y, Al(7));
EXPECT_EQ(0.5, *z.front());
EXPECT_EQ(3, g_allocCount.nAllocations);

auto const d = dynarray(std::move(z));
EXPECT_EQ(0.5, *d.front());

auto const e = dynarray(std::move(d), Al(9));
EXPECT_EQ(9, e.get_allocator().id);
EXPECT_EQ(5, g_allocCount.nAllocations);
}

template<typename Alloc>
Expand Down
2 changes: 1 addition & 1 deletion unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ TEST_F(dynarrayTest, erasePrecondCheck)
leakDetector->enabled = false;

dynarray<int> di{-2};
auto copy = di;
auto copy = dynarray(di);
ASSERT_DEATH( copy.erase(di.begin()), "" );
}

Expand Down

0 comments on commit f43bb5b

Please sign in to comment.