mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 17:27:46 +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:
parent
b754121da7
commit
a2501c3981
1 changed files with 3 additions and 7 deletions
|
@ -35,7 +35,8 @@ namespace AK {
|
|||
template<typename T, size_t Capacity>
|
||||
class CircularDeque : public CircularQueue<T, Capacity> {
|
||||
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;
|
||||
auto& slot = this->elements()[new_head];
|
||||
|
@ -44,15 +45,10 @@ public:
|
|||
else
|
||||
++this->m_size;
|
||||
|
||||
new (&slot) T(move(value));
|
||||
new (&slot) T(forward<U>(value));
|
||||
this->m_head = new_head;
|
||||
}
|
||||
|
||||
void enqueue_begin(const T& value)
|
||||
{
|
||||
enqueue_begin(T(value));
|
||||
}
|
||||
|
||||
T dequeue_end()
|
||||
{
|
||||
ASSERT(!this->is_empty());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue