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
Notes: sideshowbarker 2024-07-18 22:42:40 +09:00

View File

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