小编jar*_*itz的帖子

复制状态分配器:标准库分配器语义和内部存储器

我正在编写一组分配器,目的是将它们用于非常高性能的环境中,因此需要一些限制使用(由编译器调节,而不是运行时错误)。我一直在阅读有状态分配器的 C++11 语义以及它们如何被符合容器使用。

我在下面粘贴了一个简单的分配器,它只包含分配器对象中的一块内存。在 C++03 中,这是非法的。

template <typename T, unsigned N>
class internal_allocator {
private:
    unsigned char storage[N];
    std::size_t cursor;
public:
    typedef T value_type;
    internal_allocator() : cursor(0) {}
    ~internal_allocator() { }

    template <typename U>
    internal_allocator(const internal_allocator<U>& other) {
        // FIXME: What are the semantics here?
    }

    T* allocate(std::size_t n) {
        T* ret = static_cast<T*>(&storage[cursor]);
        cursor += n * sizeof(T);
        if (cursor > N)
            throw std::bad_alloc("Out of objects");
        return ret;
    }
    void deallocate(T*, std::size_t) {
        // Noop!
    }
};
Run Code Online (Sandbox Code Playgroud)

在 C++11 …

c++ stl allocator

5
推荐指数
1
解决办法
734
查看次数

标签 统计

allocator ×1

c++ ×1

stl ×1