Using a template to cycle through a sequence of containers I'm trying to implement a template that takes a container as parameter. The template has a getnext method that cycles through the elements in the parameter. Take note that I'm not using C++11.

Header:

#pragma once

template&lt;template&lt;class, class&gt; class TContainer, class TObject&gt;
class cycle
{
public:

    explicit cycle( TContainer&lt;TObject, std::allocator&lt;TObject&gt;&gt; &amp; container )
        : mContainer( container ), index(0) {}

    TObject getNext(int numObjectsToCycle) 
    { return mContainer[index++ % numObjectsToCycle]; }
private:
    TContainer&lt;TObject, std::allocator&lt;TObject&gt;&gt; &amp; mContainer;
    int index;
};


Implementation:

#include &lt;iostream&gt;
#include &lt;vector&gt;

#include "cycle.h"

using namespace std;


class Processor
{
   int mIndex;
   vector&lt;int&gt; numbers;

public:
   cycle&lt;vector, int&gt; cycler;
   // Is it safe to pass numbers since it is declared first??
   Processor() : mIndex(0), cycler(numbers) {} 
   void update() { cout &lt;&lt; numbers[mIndex++ % numbers.size()] &lt;&lt; std::endl;}
   void addNumber(int x) { numbers.push_back(x); }
};


int main()
{
   Processor S;

   for (int i = 0; i &lt; 5; ++i)
   {
      S.addNumber(i+1);
   }
   cout &lt;&lt; "using update" &lt;&lt; endl;
   for (int c = 0; c &lt; 10; ++c)
   {
      S.update();
   }

   cout &lt;&lt; "using cycle" &lt;&lt; endl;
   for (int c = 0; c &lt; 10; ++c)
   {
      cout &lt;&lt; S.cycler.getNext(5) &lt;&lt; endl;
   }


   std::cin.get();

}


Any improvement or potential issues to the code?
Here is how I would write it:

template&lt;typename C&gt;
class cycle
{
    C&amp; mContainer;
    size_t index;
    using ref = decltype(mContainer[0]);

public:
    explicit cycle(C&amp; container) :
        mContainer(container), index(0) {}

    ref getNext()
    {
        return mContainer[index++ % mContainer.size()];
    }
};


Notes:


The template parameter is the container, C. This not only simplifies code, but also works for containers that do not follow a specific pattern, e.g. have more parameters than just a value type and an allocator type.
The return type of getNext is automatically inferred from C. This is not just C's value_type but a (non-const) reference to it.
If I understand correctly from the title, C is supposed to be a sequence of containers? If so, the value_type is a container itself so it is even more important that a reference is returned from getNext.
Why pass a parameter to getNext? To cycle correctly, you should use mContainer.size(), exactly as you do in Processor::update, right?
index is of type size_t


With only a few changes, here is Processor:

class Processor
{
   size_t mIndex;
   std::vector&lt;int&gt; numbers;

public:
   cycle&lt;std::vector&lt;int&gt;&gt; cycler;
   Processor() : mIndex(0), numbers(), cycler(numbers) {}
   void update() { std::cout &lt;&lt; numbers[mIndex++ % numbers.size()] &lt;&lt; std::endl;}
   void addNumber(int x) { numbers.push_back(x); }
};


Notes:


again, mIndex is a size_t
cycler is a cycle&lt;std::vector&lt;int&gt;&gt;
numbers is default-initialized
cycler is safely constructed after numbers


Also, whenever you want to

using namespace std;


for convenience, only do it inside another (your own) namespace, never globally.

EDIT

I just noticed you're not using C++11. In this case, just use

    typedef typename C::value_type&amp; ref;


instead.