# Generator Iterator Adaptor

Defined in header boost/iterator/generator_iterator.hpp

The generator iterator adaptor makes it easier to create custom input iterators from 0-ary functions and function objects. The adaptor takes a Generator and creates a model of Input Iterator. Each increment retrieves an item from the generator and makes it available to be retrieved by dereferencing. The motivation for this iterator is that some concepts can be more naturally expressed as a generator, while most STL algorithms expect an iterator. An example is the Random Number library.

## Synopsis

>     namespace boost {
>     namespace iterators {
>       template <class Generator>
>       class generator_iterator_generator;

>       template <class Generator>
>       typename generator_iterator_generator<Generator>::type
>       make_generator_iterator(Generator & gen);
>     }
>     }

## The Generator Iterator Generator Class

The class generator_iterator_generator is a helper class whose purpose is to construct a generator iterator type. The template parameter for this class is the Generator function object type that is being wrapped. The generator iterator adaptor only holds a reference (or pointer) to the function object, therefore the function object must outlive the generator iterator adaptor constructed from it.

    template <class Generator>
    class generator_iterator_generator
    {
    public:
      using type = unspecified; // the resulting generator iterator type
    };

### Template Parameters

Parameter | Description
--- | ---
`Generator` | The generator (0-ary function object) type being wrapped. The return type of the function is deduced automatically from its `operator()`. The function object must be a model of Generator.

### Concept Model

The generator iterator class is a model of Input Iterator.

### Members

The generator iterator implements the member functions and operators required of the Input Iterator concept.

## The Generator Iterator Object Generator

The `make_generator_iterator()` function provides a convenient way to create generator iterator objects. The function saves the user the trouble of explicitly writing out the iterator types.

>     template <class Generator>
>     typename generator_iterator_generator<Generator>::type
>     make_generator_iterator(Generator & gen);

### Example

The following program shows how `generator_iterator` transforms a generator into an input iterator.

>     #include <iostream>
>     #include <boost/iterators/generator_iterator.hpp>

>     class my_generator
>     {
>     public:
>       using result_type = int;
>       my_generator() : state(0) { }
>       result_type operator()() { return ++state; }
>     private:
>       int state;
>     };

>     int main()
>     {
>       my_generator gen;
>       auto it = boost::iterators::make_generator_iterator(gen);
>       for(int i = 0; i < 10; ++i, ++it)
>         std::cout << *it << std::endl;
>     }

Revised 05 December, 2006

Copyright © 2001 Jens Maurer

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)