From 2490fc79ad65c6637af812baf17ab6df50c4f04d Mon Sep 17 00:00:00 2001 From: Lenny Maiorani Date: Fri, 15 Jan 2021 15:56:05 -0700 Subject: [PATCH] CircularQueue: Correctly pass args to enqueue 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/CircularQueue.h | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/AK/CircularQueue.h b/AK/CircularQueue.h index 8ef276bb37..a7f03f038c 100644 --- a/AK/CircularQueue.h +++ b/AK/CircularQueue.h @@ -60,24 +60,20 @@ public: size_t capacity() const { return Capacity; } - void enqueue(T&& value) + template + void enqueue(U&& value) { auto& slot = elements()[(m_head + m_size) % Capacity]; if (m_size == Capacity) slot.~T(); - new (&slot) T(move(value)); + new (&slot) T(forward(value)); if (m_size == Capacity) m_head = (m_head + 1) % Capacity; else ++m_size; } - void enqueue(const T& value) - { - enqueue(T(value)); - } - T dequeue() { ASSERT(!is_empty());