raw_storage_iterator |
An iterator used to directly construct objects in raw storage.
#include <memory>
template< class OutputIterator, class T >
class raw_storage_iterator : public output_iterator
#include <iostream>
#include <memory>
class X
{
public:
X( int i = 0 ) : i_( i ) {}
operator int() const { return i_; }
private:
int i_;
};
inline ostream&
operator<<( ostream& stream, const X& x )
{
return stream << "X( " << (int)x << " )";
}
void
main()
{
allocator< X > a;
// Allocate( but do not construct ) storage for 5 elements.
const int limit = 5;
X* p = a.allocate( limit );
// Construct elements in raw memory.
raw_storage_iterator< X*, X > r( p );
int i;
for ( i = 0; i < limit; ++i )
*r++ = X( i );
// Use newly constructed objects.
X* p2 = p;
for ( i = 0; i < limit; ++i )
cout << *p2++ << "\n";
// Call all destructors, then free memory.
destroy( p, p + limit );
a.deallocate( p );
}
0
1
2
3
4
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.