From a68f41102463245c10e438d925da05f95e7faf83 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 10 Oct 2024 21:51:44 +0300 Subject: [PATCH] BAN: Add requires clauses for Container::emplace{,_back} functions This allows syntax highlighters to report errors! --- BAN/include/BAN/CircularQueue.h | 4 ++-- BAN/include/BAN/HashMap.h | 6 +++--- BAN/include/BAN/LinkedList.h | 8 ++++---- BAN/include/BAN/Optional.h | 4 ++-- BAN/include/BAN/Queue.h | 4 ++-- BAN/include/BAN/RefPtr.h | 3 ++- BAN/include/BAN/UniqPtr.h | 3 ++- BAN/include/BAN/Variant.h | 2 +- BAN/include/BAN/Vector.h | 8 ++++---- 9 files changed, 22 insertions(+), 20 deletions(-) diff --git a/BAN/include/BAN/CircularQueue.h b/BAN/include/BAN/CircularQueue.h index 57a84b43..28554c5c 100644 --- a/BAN/include/BAN/CircularQueue.h +++ b/BAN/include/BAN/CircularQueue.h @@ -24,7 +24,7 @@ namespace BAN void push(const T&); void push(T&&); template - void emplace(Args&&... args); + void emplace(Args&&... args) requires is_constructible_v; void pop(); @@ -71,7 +71,7 @@ namespace BAN template template - void CircularQueue::emplace(Args&&... args) + void CircularQueue::emplace(Args&&... args) requires is_constructible_v { ASSERT(!full()); new (element_at(((m_first + m_size) % capacity()))) T(BAN::forward(args)...); diff --git a/BAN/include/BAN/HashMap.h b/BAN/include/BAN/HashMap.h index edbe35f2..6f4f7e01 100644 --- a/BAN/include/BAN/HashMap.h +++ b/BAN/include/BAN/HashMap.h @@ -14,7 +14,7 @@ namespace BAN struct Entry { template - Entry(const Key& key, Args&&... args) + Entry(const Key& key, Args&&... args) requires is_constructible_v : key(key) , value(forward(args)...) {} @@ -42,7 +42,7 @@ namespace BAN ErrorOr insert(const Key&, const T&); ErrorOr insert(const Key&, T&&); template - ErrorOr emplace(const Key&, Args&&...); + ErrorOr emplace(const Key&, Args&&...) requires is_constructible_v; iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); } iterator end() { return iterator(m_buckets.end(), m_buckets.end()); } @@ -130,7 +130,7 @@ namespace BAN template template - ErrorOr HashMap::emplace(const Key& key, Args&&... args) + ErrorOr HashMap::emplace(const Key& key, Args&&... args) requires is_constructible_v { ASSERT(!contains(key)); TRY(rebucket(m_size + 1)); diff --git a/BAN/include/BAN/LinkedList.h b/BAN/include/BAN/LinkedList.h index 79161f37..a7b5f3ad 100644 --- a/BAN/include/BAN/LinkedList.h +++ b/BAN/include/BAN/LinkedList.h @@ -34,9 +34,9 @@ namespace BAN ErrorOr insert(iterator, const T&); ErrorOr insert(iterator, T&&); template - ErrorOr emplace_back(Args&&...); + ErrorOr emplace_back(Args&&...) requires is_constructible_v; template - ErrorOr emplace(iterator, Args&&...); + ErrorOr emplace(iterator, Args&&...) requires is_constructible_v; void pop_back(); iterator remove(iterator); @@ -196,14 +196,14 @@ namespace BAN template template - ErrorOr LinkedList::emplace_back(Args&&... args) + ErrorOr LinkedList::emplace_back(Args&&... args) requires is_constructible_v { return emplace(end(), forward(args)...); } template template - ErrorOr LinkedList::emplace(iterator iter, Args&&... args) + ErrorOr LinkedList::emplace(iterator iter, Args&&... args) requires is_constructible_v { Node* new_node = TRY(allocate_node(forward(args)...)); insert_node(iter, new_node); diff --git a/BAN/include/BAN/Optional.h b/BAN/include/BAN/Optional.h index ee893319..c5932474 100644 --- a/BAN/include/BAN/Optional.h +++ b/BAN/include/BAN/Optional.h @@ -25,7 +25,7 @@ namespace BAN constexpr Optional& operator=(const Optional&); template - constexpr Optional& emplace(Args&&...); + constexpr Optional& emplace(Args&&...) requires is_constructible_v; constexpr T* operator->(); constexpr const T* operator->() const; @@ -111,7 +111,7 @@ namespace BAN template template - constexpr Optional& Optional::emplace(Args&&... args) + constexpr Optional& Optional::emplace(Args&&... args) requires is_constructible_v { clear(); m_has_value = true; diff --git a/BAN/include/BAN/Queue.h b/BAN/include/BAN/Queue.h index 5969189a..88d472ea 100644 --- a/BAN/include/BAN/Queue.h +++ b/BAN/include/BAN/Queue.h @@ -31,7 +31,7 @@ namespace BAN ErrorOr push(T&&); ErrorOr push(const T&); template - ErrorOr emplace(Args&&...); + ErrorOr emplace(Args&&...) requires is_constructible_v; ErrorOr reserve(size_type); ErrorOr shrink_to_fit(); @@ -131,7 +131,7 @@ namespace BAN template template - ErrorOr Queue::emplace(Args&&... args) + ErrorOr Queue::emplace(Args&&... args) requires is_constructible_v { TRY(ensure_capacity(m_size + 1)); new (m_data + m_size) T(forward(args)...); diff --git a/BAN/include/BAN/RefPtr.h b/BAN/include/BAN/RefPtr.h index 155de4e3..9f83ceb5 100644 --- a/BAN/include/BAN/RefPtr.h +++ b/BAN/include/BAN/RefPtr.h @@ -76,8 +76,9 @@ namespace BAN return ptr; } + // NOTE: don't use is_constructible_v as RefPtr is allowed with friends template - static ErrorOr create(Args&&... args) + static ErrorOr create(Args&&... args) requires requires(Args&&... args) { T(forward(args)...); } { T* pointer = new T(forward(args)...); if (pointer == nullptr) diff --git a/BAN/include/BAN/UniqPtr.h b/BAN/include/BAN/UniqPtr.h index 3b49f575..7d21467b 100644 --- a/BAN/include/BAN/UniqPtr.h +++ b/BAN/include/BAN/UniqPtr.h @@ -33,8 +33,9 @@ namespace BAN return uniq; } + // NOTE: don't use is_constructible_v as UniqPtr is allowed with friends template - static BAN::ErrorOr create(Args&&... args) + static BAN::ErrorOr create(Args&&... args) requires requires(Args&&... args) { T(forward(args)...); } { UniqPtr uniq; uniq.m_pointer = new T(BAN::forward(args)...); diff --git a/BAN/include/BAN/Variant.h b/BAN/include/BAN/Variant.h index d09617bb..0cdc8569 100644 --- a/BAN/include/BAN/Variant.h +++ b/BAN/include/BAN/Variant.h @@ -217,7 +217,7 @@ namespace BAN } template - void emplace(Args&&... args) requires (can_have()) + void emplace(Args&&... args) requires (can_have() && is_constructible_v) { clear(); m_index = detail::index(); diff --git a/BAN/include/BAN/Vector.h b/BAN/include/BAN/Vector.h index c58a111d..7583c175 100644 --- a/BAN/include/BAN/Vector.h +++ b/BAN/include/BAN/Vector.h @@ -35,9 +35,9 @@ namespace BAN ErrorOr push_back(T&&); ErrorOr push_back(const T&); template - ErrorOr emplace_back(Args&&...); + ErrorOr emplace_back(Args&&...) requires is_constructible_v; template - ErrorOr emplace(size_type, Args&&...); + ErrorOr emplace(size_type, Args&&...) requires is_constructible_v; ErrorOr insert(size_type, T&&); ErrorOr insert(size_type, const T&); @@ -169,7 +169,7 @@ namespace BAN template template - ErrorOr Vector::emplace_back(Args&&... args) + ErrorOr Vector::emplace_back(Args&&... args) requires is_constructible_v { TRY(ensure_capacity(m_size + 1)); new (m_data + m_size) T(forward(args)...); @@ -179,7 +179,7 @@ namespace BAN template template - ErrorOr Vector::emplace(size_type index, Args&&... args) + ErrorOr Vector::emplace(size_type index, Args&&... args) requires is_constructible_v { ASSERT(index <= m_size); TRY(ensure_capacity(m_size + 1));