std::allocator implementation

does anyone know how the default STL allocator class works, I need help implementing this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    template<typename T>
    class allocator
    {
    public:
        using size_type       = size_t;
        using difference_type = ptrdiff_t;
        using pointer         = T*;
        using const_pointer   = const T*;
        using reference       = T&;
        using const_reference = const T&;
        using value_type      = T;

        template<typename U>
            struct rebind { using other = allocator<U>; };

        allocator() noexcept;
        allocator(const allocator&) noexcept;
        template<typename U>
            allocator(const allocator<U>&) noexcept;
        ~allocator();

        pointer address(reference x) const noexcept;
        const_pointer const_address(const_reference x) const noexcept;

        pointer allocate(size_type n, allocator<void>::const_pointer hint = 0);
        void deallocate(pointer p, size_type n);

        size_type max_size() const noexcept;

        template<typename U, typename... Args>
            void construct(U* p, Args&&... args);

        template<typename U>
            void destroy(U* p);
    };

help!
Last edited on
for one thing, it doesn't need so many members, since the defaults for almost all of them are supplied by allocator_traits.

Did you look at the existing implementations?
clang's libc++ here: https://github.com/llvm-mirror/libcxx/blob/master/include/memory#L1819-L1921
gnu libstdc++ here: https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/allocator.h#L115-L216
@Cubbi thanks I'll have a look at those.
Topic archived. No new replies allowed.