I am writing a function for the Sieve of Erosthenes that returns a list of primes less than a given number n. This algorithm is very well known to mathemeticians. I'm perhaps making it a bit more complicated by making a template function so that the number n and the primes in the list can be any appropriate type, such as int, long, or some kind of BigInt class. In other words, I want my implementation to be very generic.

The declaration for the fuction looks like this:

Code:

template <class T>
std::list<T> primes(T n);

As part of the implementation, I have a list to hold all the numbers from 2 to n:

Later I want to get an iterator to this list, which is where I encounter a problem. My first attempt at declaring the iterator was

Code:

std::list<T>::iterator itr;

This gives the following error:

Code:

In function '__gnu_debug_def::list<T, std::allocator<_CharT> > primes(T)':
Line 76: error: expected `;' before 'itr'

Since I am currently testing my code with longs, I've tried

Code:

std::list<long>::iterator itr;

which works beautifully. However, I need this to be more generic so I can reuse my code for different integral types. How do I declare this iterator with a template parameter?