More C++ Idioms/Free Function Allocators

From Wikibooks, open books for an open world
Jump to: navigation, search

As seen in boost::ptr_container

Motivation: allocators have serious problems, because they change the underlying type of the container.


struct user_allocator_nedmalloc
{
        typedef std::size_t size_type;
        typedef std::ptrdiff_t difference_type;
 
        static inline char* malloc(const size_type bytes) {
                return reinterpret_cast<char*>(nedmalloc(bytes));
        }
 
        static inline void free(char* const block) {
                nedfree(block);
        }
};

This idiom is wildly superior to the way that std::allocators work the whole idiom is outlined here:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1850.pdf

To do:
someone please distill that whole thing into one short idiom

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export