More C++ Idioms/Free Function Allocators

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

Free Function Allocators
[edit | edit source]

Intent[edit | edit source]

Allow containers to use custom allocators without creating a new type

Also Known As[edit | edit source]

Motivation[edit | edit source]

C++ standard allocators have serious problems, because they change the underlying type of the container.

Solution and Sample Code[edit | edit source]

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

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) {

Known Uses[edit | edit source]

  • boost::ptr_container

Related Idioms[edit | edit source]

References[edit | edit source]