1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

SinglyLinkedListWithCount: Correctly pass args to append, insert_before, insert_after

Problem:
- Using regular functions rather than function templates results in
  the arguments not being deduced. This then requires the same
  function to be written multiple times and for `move` to be used
  rather than `forward`.

Solution:
- Collapse multiple function overloads to a single function template
  with a deduced argument. This allows the argument to be a forwarding
  reference and bind to either an l-value or r-value and forward the
  value.
This commit is contained in:
Lenny Maiorani 2021-01-15 09:51:05 -07:00 committed by Andreas Kling
parent 6e4e3a7612
commit 5cee5725e7

View file

@ -80,16 +80,11 @@ public:
return List::take_first(); return List::take_first();
} }
void append(const T& value) template<typename U = T>
void append(U&& value)
{ {
m_count++; m_count++;
return SinglyLinkedList<T>::append(value); return List::append(forward<T>(value));
}
void append(T&& value)
{
m_count++;
return List::append(move(value));
} }
bool contains_slow(const T& value) const bool contains_slow(const T& value) const
@ -135,28 +130,18 @@ public:
return List::remove(iterator); return List::remove(iterator);
} }
void insert_before(Iterator iterator, const T& value) template<typename U = T>
void insert_before(Iterator iterator, U&& value)
{ {
m_count++; m_count++;
List::insert_before(iterator, value); List::insert_before(iterator, forward<T>(value));
} }
void insert_before(Iterator iterator, T&& value) template<typename U = T>
void insert_after(Iterator iterator, U&& value)
{ {
m_count++; m_count++;
List::insert_before(iterator, move(value)); List::insert_after(iterator, forward<T>(value));
}
void insert_after(Iterator iterator, const T& value)
{
m_count++;
List::insert_after(iterator, value);
}
void insert_after(Iterator iterator, T&& value)
{
m_count++;
List::insert_after(iterator, move(value));
} }
private: private: