Thread: Fun with templates

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Idaho
    Posts
    2

    Fun with templates

    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:

    Code:
    std::list<T> nums;
    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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write:
    Code:
    typename std::list<T>::iterator itr;
    Basically, when you see a type name that is nested such that it depends on the template parameter, you use typename, otherwise the compiler assumes that the name is a member name, not a type name.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Idaho
    Posts
    2
    Sweet! That worked like a charm. Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates. Help!
    By hanniball in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2008, 10:32 AM
  2. Help with templates
    By hanniball in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 06:16 PM
  3. c++ Templates
    By micha_mondeli in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2005, 06:43 PM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM
  5. Need help with templates
    By Flyer in forum C++ Programming
    Replies: 3
    Last Post: 06-30-2003, 10:12 AM

Tags for this Thread