From 5cee5725e74d5ffa6ff696a1a5f434d52fed4949 Mon Sep 17 00:00:00 2001 From: Lenny Maiorani Date: Fri, 15 Jan 2021 09:51:05 -0700 Subject: [PATCH] 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. --- AK/SinglyLinkedListWithCount.h | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/AK/SinglyLinkedListWithCount.h b/AK/SinglyLinkedListWithCount.h index a84762bae4..1ef8c1273e 100644 --- a/AK/SinglyLinkedListWithCount.h +++ b/AK/SinglyLinkedListWithCount.h @@ -80,16 +80,11 @@ public: return List::take_first(); } - void append(const T& value) + template + void append(U&& value) { m_count++; - return SinglyLinkedList::append(value); - } - - void append(T&& value) - { - m_count++; - return List::append(move(value)); + return List::append(forward(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 + void insert_before(Iterator iterator, U&& value) { m_count++; - List::insert_before(iterator, value); + List::insert_before(iterator, forward(value)); } - void insert_before(Iterator iterator, T&& value) + template + 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(value)); } private: