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:
As part of the implementation, I have a list to hold all the numbers from 2 to n:
template <class T>
std::list<T> primes(T 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
This gives the following error:
Since I am currently testing my code with longs, I've tried
In function '__gnu_debug_def::list<T, std::allocator<_CharT> > primes(T)':
Line 76: error: expected `;' before '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?