While maybe not "more generic", if you don't want to handle all the allocation yourself, you could inherit from the standard allocator std::allocator
:
template<class T, class S = MPLLIBS_STRING("unknown"), class Allocator = std::allocator<T>>
struct LoggingAllocator : public Allocator {
// ...
};
In the allocate
/destroy
/deallocate
functions do the logging, and then call the parents methods:
pointer allocate (size_type n, std::allocator<void>::const_pointer hint = 0) {
log_allocation(boost::mpl::c_str<S>::value);
return Allocator::allocate(n, hint);
}
However note that std::allocator
isn't really designed for being inherited, exemplified by it having no virtual destructor.