fixed swap()

This commit is contained in:
Patrick 2024-07-29 18:31:05 +02:00
parent c2d828cfb9
commit 32ae9a2d3d
2 changed files with 13 additions and 13 deletions

View File

@ -527,13 +527,13 @@ namespace cc {
template<typename ...Args>
constexpr const_vector<T, N>::reference const_vector<T, N>::emplace_back(Args&&... args)
{
emplace(end(), std::forward(args)...);
return *emplace(cend(), std::forward<Args>(args)...);
}
template<typename T, std::size_t N>
constexpr void const_vector<T, N>::pop_back()
{
_erase_no_move(end() - 1, end());
std::destroy_n(end() - 1, 1);
--_size;
}
@ -541,9 +541,9 @@ namespace cc {
template<std::size_t N2>
constexpr void const_vector<T, N>::swap(const_vector<T, N2> &other)
{
if (_size > other._len || other._size > N) throw std::exception();
if (_size > other._len || other._size > N) throw std::invalid_argument("Cannot swap elements, one does not fit into the other");
cc::helper::swap_iter_range(begin(), end(), std::end(_arr), other.begin(), other.end(), std::end(other._arr));
cc::helper::swap_iter_range(begin(), end(), other.begin(), other.end());
std::swap(_size, other._size);
}

View File

@ -15,10 +15,10 @@ namespace cc::helper {
template<typename T, std::size_t N>
constexpr std::size_t array_size(const T(&)[N]) { return N; }
template<std::input_or_output_iterator InputIt1, std::input_or_output_iterator InputIt2, typename T = InputIt1::value_type>
requires ((InputIt1::value_type == InputIt2::value_type)
&& std::indirectly_writable<typename InputIt1::value_type, InputIt1> && std::indirectly_readable<InputIt1>
&& std::indirectly_writable<typename InputIt2::value_type, InputIt1> && std::indirectly_readable<InputIt2>)
template<std::input_or_output_iterator InputIt1, std::input_or_output_iterator InputIt2>
requires std::is_same_v<typename std::iterator_traits<InputIt1>::value_type, typename std::iterator_traits<InputIt2>::value_type>
&& std::indirectly_writable<InputIt1, typename std::iterator_traits<InputIt2>::value_type> && std::indirectly_readable<InputIt1>
&& std::indirectly_writable<InputIt2, typename std::iterator_traits<InputIt1>::value_type> && std::indirectly_readable<InputIt2>
constexpr void swap_iter_range(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
{
auto dist1 = std::distance(first1, last1);
@ -31,7 +31,7 @@ namespace cc::helper {
auto swap_dist = std::min(dist1, dist2);
std::swap_ranges(first1, std::next(first1, swap_dist), first2, std::next(first2, swap_dist));
std::swap_ranges(first1, std::next(first1, swap_dist), first2);
std::advance(first1, swap_dist);
std::advance(first2, swap_dist);
@ -43,10 +43,10 @@ namespace cc::helper {
}
}
template<std::input_or_output_iterator InputIt1, std::input_or_output_iterator InputIt2, typename T = InputIt1::value_type>
requires ((InputIt1::value_type == InputIt2::value_type)
&& std::indirectly_writable<typename InputIt1::value_type, InputIt1> && std::indirectly_readable<InputIt1>
&& std::indirectly_writable<typename InputIt2::value_type, InputIt1> && std::indirectly_readable<InputIt2>)
template<std::input_or_output_iterator InputIt1, std::input_or_output_iterator InputIt2>
requires std::is_same_v<typename std::iterator_traits<InputIt1>::value_type, typename std::iterator_traits<InputIt2>::value_type>
&& std::indirectly_writable<InputIt1, typename std::iterator_traits<InputIt2>::value_type> && std::indirectly_readable<InputIt1>
&& std::indirectly_writable<InputIt2, typename std::iterator_traits<InputIt1>::value_type> && std::indirectly_readable<InputIt2>
constexpr void swap(InputIt1 first1, InputIt1 last1, InputIt1 end1, InputIt2 first2, InputIt2 last2, InputIt2 end2)
{
auto max_dist = std::min(std::distance(first1, end1), std::distance(first2, end2));