Advertisement
Guest User

Untitled

a guest
May 11th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. #include <vector>
  2.  
  3. struct xyz {
  4.     // empty constructor: should leave 'v' uninitialized!
  5.     xyz() { }
  6.     xyz(const xyz& o): v(o.v) { }
  7.     xyz& operator=(const xyz& o) { v=o.v; return *this; }
  8.     int v;
  9. };
  10.  
  11. template <typename T>
  12. class uninitialized_allocator : public std::allocator< T > {
  13.   private:
  14.     bool* should_init;
  15.   public:
  16.     //provide the required no-throw constructors / destructors:
  17.     uninitialized_allocator() throw() : std::allocator<xyz>() { };
  18.     uninitialized_allocator(const uninitialized_allocator<T>& rhs) throw() : std::allocator<T>(rhs) { };
  19.     template <typename U>
  20.     uninitialized_allocator(const std::allocator<U>& rhs) throw() : std::allocator<T>(rhs) { };
  21.     ~uninitialized_allocator() throw() { };
  22.  
  23.     //import the required typedefs:
  24.     typedef typename std::allocator<T>::value_type value_type;
  25.     typedef typename std::allocator<T>::pointer pointer;
  26.     typedef typename std::allocator<T>::reference reference;
  27.     typedef typename std::allocator<T>::const_pointer const_pointer;
  28.     typedef typename std::allocator<T>::const_reference const_reference;
  29.     typedef typename std::allocator<T>::size_type size_type;
  30.     typedef typename std::allocator<T>::difference_type difference_type;
  31.  
  32.     //redefine the construct function (hiding the base-class version):
  33.     void construct( pointer p, const_reference o) {
  34.     };
  35. };
  36.  
  37. typedef std::vector<xyz, uninitialized_allocator<xyz> > myvec_t;
  38.  
  39. extern myvec_t test() {
  40.     return myvec_t(1024);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement