How do you determine the size of the nodes created by a 'std::map' for use with 'boost::pool_allocator' (in a cross-platform way)?

后端 未结 2 1237
轮回少年
轮回少年 2020-12-18 06:51

UPDATE

Per comments, answer, and additional research, I have come to the conclusion that there is typically no difference between a set

2条回答
  •  失恋的感觉
    2020-12-18 07:36

    The problem is that you don't know the internal type which set uses for nodes.

    While I haven't figured out how to determine this at compile time, you can write a tracing allocator which prints out the sizeof the node type when allocate is called, as in:

    template
    struct SimpleAllocator : private std::allocator
    {
        using value_type = T;
        using pointer = typename std::allocator::pointer;
        using size_type = typename std::allocator::size_type;
    
        pointer allocate(size_type n)
        {   
            std::cout << "Allocator sizeof(T)==" << sizeof(T) << '\n';
            return std::allocator::allocate(n);
        }   
    
        void deallocate(pointer p, size_type n)
        { return std::allocator::deallocate(p, n); }
    };
    

    And a little test program (I'm testing with sets of ints):

    std::set, SimpleAllocator> s;
    s.insert(2);
    

    On my system, I get output of:

    Allocator sizeof(T)==32

提交回复
热议问题