首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解开类型依赖关系?(X使用未定义的Y类)

如何解开类型依赖关系?(X使用未定义的Y类)
EN

Stack Overflow用户
提问于 2022-02-07 16:24:26
回答 1查看 55关注 0票数 1

我试图为我的委托类型实现简单的快速内存管理,但是遇到了循环依赖,而这是我自己解决不了的。

代码语言:javascript
复制
// 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>'“。如何打破这种循环依赖关系?我尝试使用更多的指针,将桶移到委托范围之外,但是这个错误仍然存在。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-07 16:42:54

通过将bucket结构的定义移到Delegate范围之外,并将_alloc_buffer_current_bucket变量分配到类范围之外,我们可以实现编译。我能够把它编译成这样:

代码语言:javascript
复制
// 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上测试这个最小样本。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71021895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档