我试图为我的委托类型实现简单的快速内存管理,但是遇到了循环依赖,而这是我自己解决不了的。
// Size of single bucket of delegates
static constexpr size_t _alloc_buffer_bucket_size = 128;
template <typename TFunc, typename... Args>
struct bucket;
template <typename TFunc, typename... Args>
class Delegate final : public IDelegate
{
. . .
static void* operator new (size_t size)
{
auto bckt = _current_bucket; // gives compilation error here
}
private:
// Size of single bucket of delegates
static constexpr size_t _alloc_buffer_bucket_size = 128;
struct bucket
{
// stores currently occupied slots in bucket.
size_t allocated_slots = 0;
// when this bucket is filling with values, this
size_t current_slot = 0;
std::array<Delegate<TFunc, Args...>, _alloc_buffer_bucket_size> slots = {};
};
// Vector of buckets.
inline static std::vector<bucket*> _alloc_buffer = { new bucket(), };
// This is a pointer to currently filling bucket. New delegates are added here
// by delegate allocator.
// When this bucket reaches _alloc_buffer_bucket_size of items, this pointer is
// replaced with either newly allocated bucket or existing and empty bucket.
inline static bucket* _current_bucket = &(_alloc_buffer[0]);
};这给了我"'Delegate<void (*)(void)>::bucket::slots' uses undefined class 'std::array<Delegate<void (*)(void)>,128>'“。如何打破这种循环依赖关系?我尝试使用更多的指针,将桶移到委托范围之外,但是这个错误仍然存在。
发布于 2022-02-07 16:42:54
通过将bucket结构的定义移到Delegate范围之外,并将_alloc_buffer和_current_bucket变量分配到类范围之外,我们可以实现编译。我能够把它编译成这样:
// Size of single bucket of delegates
static constexpr std::size_t _alloc_buffer_bucket_size = 128;
template <typename TFunc, typename... Args>
struct bucket;
template <typename TFunc, typename... Args>
class Delegate final : public IDelegate
{
public:
Delegate() = default;
static void* operator new (std::size_t size)
{
auto bckt = _current_bucket; // gives compilation error here
return nullptr; // added to get rid of the error
}
private:
// Vector of buckets.
static std::vector<bucket<TFunc, Args...>*> _alloc_buffer;
// This is a pointer to currently filling bucket. New delegates are added here
// by delegate allocator.
// When this bucket reaches _alloc_buffer_bucket_size of items, this pointer is
// replaced with either newly allocated bucket or existing and empty bucket.
static bucket<TFunc, Args...>* _current_bucket;
};
template <typename TFunc, typename... Args>
struct bucket
{
// stores currently occupied slots in bucket.
std::size_t allocated_slots = 0;
// when this bucket is filling with values, this
std::size_t current_slot = 0;
std::array<Delegate<TFunc, Args...>, _alloc_buffer_bucket_size> slots = {};
};
template <typename TFunc, typename... Args>
std::vector<bucket<TFunc, Args...>*> Delegate<TFunc, Args...>::_alloc_buffer = { new bucket<TFunc, Args...>(), };
template <typename TFunc, typename... Args>
bucket<TFunc, Args...>* Delegate<TFunc, Args...>::_current_bucket = &(_alloc_buffer[0]);通过拆分静态变量的声明和定义,并将桶类型拆分,Delegate类是在我们实际使用它的时候定义的。在这里看到它的作用:https://godbolt.org/z/bx7hh1av9
编辑:在MSVC和Clang上测试这个最小样本。
https://stackoverflow.com/questions/71021895
复制相似问题