Skip to content

Commit 328c167

Browse files
committed
[Util] Add is_allocator concept
Without this concept, compiler would pick the templated perfect-forwarding constructor of `doubly_linked_list` over its copy constructor for `non const` arguments.
1 parent 4208f74 commit 328c167

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

include/mutable/util/ADT.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ struct dyn_array
365365
/** Implements a doubly-linked list with an overhead of just a single pointer per element. */
366366
template<
367367
typename T,
368-
typename Allocator = malloc_allocator
368+
is_allocator Allocator = malloc_allocator
369369
>
370370
struct doubly_linked_list
371371
{
@@ -459,7 +459,7 @@ struct doubly_linked_list
459459
/*----- Constructors & Destructor --------------------------------------------------------------------------------*/
460460
doubly_linked_list() : doubly_linked_list(allocator_type()) { }
461461

462-
template<typename A = allocator_type>
462+
template<is_allocator A = allocator_type>
463463
explicit doubly_linked_list(A &&allocator)
464464
: allocator_(std::forward<A>(allocator))
465465
{ }

include/mutable/util/allocator_base.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <memory>
45
#include <type_traits>
56

67

78
namespace m {
89

10+
template <typename T>
11+
concept is_allocator = requires (T t, size_t size, size_t alignment, void *ptr) {
12+
{ t.allocate(size, alignment) } -> std::same_as<void*>;
13+
{ t.deallocate(ptr, size) } -> std::same_as<void>;
14+
};
15+
916
template<typename Actual>
1017
struct allocator
1118
{

unittest/util/ADTTest.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,10 +592,11 @@ TEST_CASE("doubly_linked_list", "[core][util]")
592592

593593
SECTION("copy c'tor")
594594
{
595-
const dlint L1(vec.begin(), vec.end());
595+
dlint L1(vec.begin(), vec.end());
596+
dlint& L1_ref = L1;
596597

597598
dlint L2(L1);
598-
dlint L3 = dlint(L1);
599+
dlint L3 = dlint(L1_ref);
599600

600601
CHECK_LIST(L1, { 42, 13, 73 });
601602
CHECK_LIST(L2, { 42, 13, 73 });
@@ -617,7 +618,7 @@ TEST_CASE("doubly_linked_list", "[core][util]")
617618

618619
SECTION("= operator")
619620
{
620-
const dlint L1(vec.begin(), vec.end());
621+
dlint L1(vec.begin(), vec.end());
621622
dlint L2;
622623

623624
L2 = dlint(L1);

0 commit comments

Comments
 (0)