#include #include #include template class echo_allocator : public std::allocator { typedef std::allocator T_allocator; public: typedef size_t size_type; typedef ptrdiff_t difference_type; typedef T_type* pointer; typedef const T_type* const_pointer; typedef T_type& reference; typedef const T_type& const_reference; typedef T_type value_type; template struct rebind { typedef echo_allocator other; }; echo_allocator() throw() { std::cerr << "*** Allocator created.\n"; } echo_allocator(const echo_allocator&) throw() { std::cerr << "*** Allocator cloned.\n"; } template echo_allocator(const echo_allocator&) throw() { std::cerr << "*** Allocator cloned with rebind.\n"; } ~echo_allocator() throw() { std::cerr << "*** Allocator destroyed.\n"; } pointer address(reference __x) const { return &__x; } const_pointer address(const_reference __x) const { return &__x; } // NB: __n is permitted to be 0. The C++ standard says nothing // about what the return value is when __n == 0. T_type* allocate(size_type __n, const void* __p = 0) { std::cerr << "+++ Allocated object.\n"; T_type *o = T_allocator::allocate(__n,__p); } // __p is not permitted to be a null pointer. void deallocate(pointer __p, size_type __n) { std::cerr << "+++ Delocated object.\n"; T_allocator::deallocate(__p,__n); } size_type max_size() const throw() { return(T_allocator::max_size()); } void construct(pointer __p, const T_type& __val) { T_allocator::construct(__p,__val); std::cerr << "+++ Constructed object.\n"; } void destroy(pointer __p) { std::cerr << "+++ Destroyed object.\n"; T_allocator::destroy(__p); } }; template class echo_list : public std::list > {}; template class echo_vector : public std::vector > {}; class A { public: A() { std::cerr << "--- New default A.\n"; _v = 0; }; A(int v) { std::cerr << "--- New A " << v << ".\n"; _v = v; }; A(const A& a) { *this = a; std::cerr << "--- Clone A " << _v << ".\n"; }; ~A() { std::cerr << "--- Del A " << _v << ".\n"; }; int _v; }; int main(int argc,char **argv) { echo_list E; E.push_back(A(4)); E.push_back(A(5)); E.push_back(A(6)); E.pop_front(); echo_vector V; V.resize(5); V[3] = A(7); }