concepts: allocator_like

This commit is contained in:
skypjack
2026-02-06 18:33:57 +01:00
parent bde5d86690
commit ed590b7ac8
2 changed files with 18 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
#ifndef ENTT_CORE_CONCEPTS_HPP
#define ENTT_CORE_CONCEPTS_HPP
#include <concepts>
#include <type_traits>
namespace entt {
@@ -12,6 +13,16 @@ namespace entt {
template<typename Type>
concept cvref_unqualified = std::is_same_v<std::remove_cvref_t<Type>, Type>;
/**
* @brief Specifies that a type is likely an allocator type.
* @tparam Type Type to check.
*/
template<typename Type>
concept allocator_like = requires(Type alloc, typename Type::value_type *value) {
{ alloc.allocate(0) } -> std::same_as<decltype(value)>;
{ alloc.deallocate(value, 0) } -> std::same_as<void>;
};
} // namespace entt
#endif

View File

@@ -12,3 +12,10 @@ TEST(Concepts, CVRefUnqualified) {
ASSERT_FALSE(entt::cvref_unqualified<const std::shared_ptr<int>>);
ASSERT_FALSE(entt::cvref_unqualified<std::shared_ptr<int> &>);
}
TEST(Concepts, AllocatorLike) {
ASSERT_FALSE(entt::allocator_like<int>);
ASSERT_TRUE(entt::allocator_like<std::allocator<int>>);
ASSERT_TRUE(entt::allocator_like<std::allocator<void>>);
ASSERT_FALSE(entt::allocator_like<std::shared_ptr<int>>);
}