1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-29 12:17:36 +00:00

CircularDeque: 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.
This commit is contained in:
Lenny Maiorani 2021-01-15 15:55:36 -07:00 committed by Andreas Kling
parent b754121da7
commit a2501c3981

View file

@ -35,7 +35,8 @@ namespace AK {
template<typename T, size_t Capacity> template<typename T, size_t Capacity>
class CircularDeque : public CircularQueue<T, Capacity> { class CircularDeque : public CircularQueue<T, Capacity> {
public: public:
void enqueue_begin(T&& value) template<typename U = T>
void enqueue_begin(U&& value)
{ {
const auto new_head = (this->m_head - 1 + Capacity) % Capacity; const auto new_head = (this->m_head - 1 + Capacity) % Capacity;
auto& slot = this->elements()[new_head]; auto& slot = this->elements()[new_head];
@ -44,15 +45,10 @@ public:
else else
++this->m_size; ++this->m_size;
new (&slot) T(move(value)); new (&slot) T(forward<U>(value));
this->m_head = new_head; this->m_head = new_head;
} }
void enqueue_begin(const T& value)
{
enqueue_begin(T(value));
}
T dequeue_end() T dequeue_end()
{ {
ASSERT(!this->is_empty()); ASSERT(!this->is_empty());