BAN: Add requires clauses for Container::emplace{,_back} functions

This allows syntax highlighters to report errors!
This commit is contained in:
Bananymous 2024-10-10 21:51:44 +03:00
parent 166550fbba
commit a68f411024
9 changed files with 22 additions and 20 deletions

View File

@ -24,7 +24,7 @@ namespace BAN
void push(const T&); void push(const T&);
void push(T&&); void push(T&&);
template<typename... Args> template<typename... Args>
void emplace(Args&&... args); void emplace(Args&&... args) requires is_constructible_v<T, Args...>;
void pop(); void pop();
@ -71,7 +71,7 @@ namespace BAN
template<typename T, size_t S> template<typename T, size_t S>
template<typename... Args> template<typename... Args>
void CircularQueue<T, S>::emplace(Args&&... args) void CircularQueue<T, S>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{ {
ASSERT(!full()); ASSERT(!full());
new (element_at(((m_first + m_size) % capacity()))) T(BAN::forward<Args>(args)...); new (element_at(((m_first + m_size) % capacity()))) T(BAN::forward<Args>(args)...);

View File

@ -14,7 +14,7 @@ namespace BAN
struct Entry struct Entry
{ {
template<typename... Args> template<typename... Args>
Entry(const Key& key, Args&&... args) Entry(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
: key(key) : key(key)
, value(forward<Args>(args)...) , value(forward<Args>(args)...)
{} {}
@ -42,7 +42,7 @@ namespace BAN
ErrorOr<void> insert(const Key&, const T&); ErrorOr<void> insert(const Key&, const T&);
ErrorOr<void> insert(const Key&, T&&); ErrorOr<void> insert(const Key&, T&&);
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace(const Key&, Args&&...); ErrorOr<void> emplace(const Key&, Args&&...) requires is_constructible_v<T, Args...>;
iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); } iterator begin() { return iterator(m_buckets.end(), m_buckets.begin()); }
iterator end() { return iterator(m_buckets.end(), m_buckets.end()); } iterator end() { return iterator(m_buckets.end(), m_buckets.end()); }
@ -130,7 +130,7 @@ namespace BAN
template<typename Key, typename T, typename HASH> template<typename Key, typename T, typename HASH>
template<typename... Args> template<typename... Args>
ErrorOr<void> HashMap<Key, T, HASH>::emplace(const Key& key, Args&&... args) ErrorOr<void> HashMap<Key, T, HASH>::emplace(const Key& key, Args&&... args) requires is_constructible_v<T, Args...>
{ {
ASSERT(!contains(key)); ASSERT(!contains(key));
TRY(rebucket(m_size + 1)); TRY(rebucket(m_size + 1));

View File

@ -34,9 +34,9 @@ namespace BAN
ErrorOr<void> insert(iterator, const T&); ErrorOr<void> insert(iterator, const T&);
ErrorOr<void> insert(iterator, T&&); ErrorOr<void> insert(iterator, T&&);
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace_back(Args&&...); ErrorOr<void> emplace_back(Args&&...) requires is_constructible_v<T, Args...>;
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace(iterator, Args&&...); ErrorOr<void> emplace(iterator, Args&&...) requires is_constructible_v<T, Args...>;
void pop_back(); void pop_back();
iterator remove(iterator); iterator remove(iterator);
@ -196,14 +196,14 @@ namespace BAN
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace_back(Args&&... args) ErrorOr<void> LinkedList<T>::emplace_back(Args&&... args) requires is_constructible_v<T, Args...>
{ {
return emplace(end(), forward<Args>(args)...); return emplace(end(), forward<Args>(args)...);
} }
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
ErrorOr<void> LinkedList<T>::emplace(iterator iter, Args&&... args) ErrorOr<void> LinkedList<T>::emplace(iterator iter, Args&&... args) requires is_constructible_v<T, Args...>
{ {
Node* new_node = TRY(allocate_node(forward<Args>(args)...)); Node* new_node = TRY(allocate_node(forward<Args>(args)...));
insert_node(iter, new_node); insert_node(iter, new_node);

View File

@ -25,7 +25,7 @@ namespace BAN
constexpr Optional& operator=(const Optional&); constexpr Optional& operator=(const Optional&);
template<typename... Args> template<typename... Args>
constexpr Optional& emplace(Args&&...); constexpr Optional& emplace(Args&&...) requires is_constructible_v<T, Args...>;
constexpr T* operator->(); constexpr T* operator->();
constexpr const T* operator->() const; constexpr const T* operator->() const;
@ -111,7 +111,7 @@ namespace BAN
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
constexpr Optional<T>& Optional<T>::emplace(Args&&... args) constexpr Optional<T>& Optional<T>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{ {
clear(); clear();
m_has_value = true; m_has_value = true;

View File

@ -31,7 +31,7 @@ namespace BAN
ErrorOr<void> push(T&&); ErrorOr<void> push(T&&);
ErrorOr<void> push(const T&); ErrorOr<void> push(const T&);
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace(Args&&...); ErrorOr<void> emplace(Args&&...) requires is_constructible_v<T, Args...>;
ErrorOr<void> reserve(size_type); ErrorOr<void> reserve(size_type);
ErrorOr<void> shrink_to_fit(); ErrorOr<void> shrink_to_fit();
@ -131,7 +131,7 @@ namespace BAN
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
ErrorOr<void> Queue<T>::emplace(Args&&... args) ErrorOr<void> Queue<T>::emplace(Args&&... args) requires is_constructible_v<T, Args...>
{ {
TRY(ensure_capacity(m_size + 1)); TRY(ensure_capacity(m_size + 1));
new (m_data + m_size) T(forward<Args>(args)...); new (m_data + m_size) T(forward<Args>(args)...);

View File

@ -76,8 +76,9 @@ namespace BAN
return ptr; return ptr;
} }
// NOTE: don't use is_constructible_v<T, Args...> as RefPtr<T> is allowed with friends
template<typename... Args> template<typename... Args>
static ErrorOr<RefPtr> create(Args&&... args) static ErrorOr<RefPtr> create(Args&&... args) requires requires(Args&&... args) { T(forward<Args>(args)...); }
{ {
T* pointer = new T(forward<Args>(args)...); T* pointer = new T(forward<Args>(args)...);
if (pointer == nullptr) if (pointer == nullptr)

View File

@ -33,8 +33,9 @@ namespace BAN
return uniq; return uniq;
} }
// NOTE: don't use is_constructible_v<T, Args...> as UniqPtr<T> is allowed with friends
template<typename... Args> template<typename... Args>
static BAN::ErrorOr<UniqPtr> create(Args&&... args) static BAN::ErrorOr<UniqPtr> create(Args&&... args) requires requires(Args&&... args) { T(forward<Args>(args)...); }
{ {
UniqPtr uniq; UniqPtr uniq;
uniq.m_pointer = new T(BAN::forward<Args>(args)...); uniq.m_pointer = new T(BAN::forward<Args>(args)...);

View File

@ -217,7 +217,7 @@ namespace BAN
} }
template<typename T, typename... Args> template<typename T, typename... Args>
void emplace(Args&&... args) requires (can_have<T>()) void emplace(Args&&... args) requires (can_have<T>() && is_constructible_v<T, Args...>)
{ {
clear(); clear();
m_index = detail::index<T, Ts...>(); m_index = detail::index<T, Ts...>();

View File

@ -35,9 +35,9 @@ namespace BAN
ErrorOr<void> push_back(T&&); ErrorOr<void> push_back(T&&);
ErrorOr<void> push_back(const T&); ErrorOr<void> push_back(const T&);
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace_back(Args&&...); ErrorOr<void> emplace_back(Args&&...) requires is_constructible_v<T, Args...>;
template<typename... Args> template<typename... Args>
ErrorOr<void> emplace(size_type, Args&&...); ErrorOr<void> emplace(size_type, Args&&...) requires is_constructible_v<T, Args...>;
ErrorOr<void> insert(size_type, T&&); ErrorOr<void> insert(size_type, T&&);
ErrorOr<void> insert(size_type, const T&); ErrorOr<void> insert(size_type, const T&);
@ -169,7 +169,7 @@ namespace BAN
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
ErrorOr<void> Vector<T>::emplace_back(Args&&... args) ErrorOr<void> Vector<T>::emplace_back(Args&&... args) requires is_constructible_v<T, Args...>
{ {
TRY(ensure_capacity(m_size + 1)); TRY(ensure_capacity(m_size + 1));
new (m_data + m_size) T(forward<Args>(args)...); new (m_data + m_size) T(forward<Args>(args)...);
@ -179,7 +179,7 @@ namespace BAN
template<typename T> template<typename T>
template<typename... Args> template<typename... Args>
ErrorOr<void> Vector<T>::emplace(size_type index, Args&&... args) ErrorOr<void> Vector<T>::emplace(size_type index, Args&&... args) requires is_constructible_v<T, Args...>
{ {
ASSERT(index <= m_size); ASSERT(index <= m_size);
TRY(ensure_capacity(m_size + 1)); TRY(ensure_capacity(m_size + 1));