class template
<memory>

std::raw_storage_iterator

template <class OutputIterator, class T>  class raw_storage_iterator;
Raw storage iterator
This iterator class operates on uninitialized memory blocks.

Regular iterators operate on a certain type of objects, which have already been constructed. A raw_storage_iterator wraps one of these regular iterators into a special output iterator which constructs objects at the location being pointed before being written.

It is defined with the same behavior as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class OutputIterator, class T>
  class raw_storage_iterator :
    public iterator<output_iterator_tag,void,void,void,void>
{
protected:
  OutputIterator iter_;

public:
  explicit raw_storage_iterator (OutputIterator x) : iter_(x) {}
  raw_storage_iterator<OutputIterator,T>& operator* ()
    { return *this; }
  raw_storage_iterator<OutputIterator,T>& operator= (const T& element)
    { new (static_cast<void*>(&*iter_)) T (element); return *this; }
  raw_storage_iterator<OutputIterator,T>& operator++ ()
    { ++iter_; return *this; }
  raw_storage_iterator<OutputIterator,T> operator++ (int)
    { raw_storage_iterator<OutputIterator,T> tmp = *this; ++iter_; return tmp; }
};

Template parameters

OutputIterator
Underlying iterator type.
T
Type of objects to be constructed on each element location.

Member functions

constructor
raw_storage_iterator objects are constructed from an iterator.
operator*
Does nothing. Returns a reference to the object.
operator=
Constructs a new object of type T at the location pointed by the iterator and initializes its value to a copy of the argument used as right-hand side of the operator.
operator++
Increases the iterator location.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// raw_storage_iterator example
#include <iostream>
#include <memory>
#include <vector>
#include <string>

int main () {
  std::vector<std::string> myvector;
  myvector.push_back ("first");
  myvector.push_back ("second");
  myvector.push_back ("third");

  std::pair<std::string*,std::ptrdiff_t> result = std::get_temporary_buffer<std::string>(3);
  std::string* pstr=result.first;

  std::raw_storage_iterator<std::string*,std::string> raw_it (pstr);

  copy (myvector.begin(), myvector.end(), raw_it);

  for (int i=0; i<3; i++)
    std::cout << pstr[i] << ' ';
  std::cout << '\n';

  std::return_temporary_buffer(pstr);

  return 0;
}

Output:

first second third 


See also