Thread: Templates trouble

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    25

    Templates trouble

    Hello, everyone! This might seem an easy question to you, and I wouldn't even post it if it wasn't for my love of clean code. So here goes:

    What I want to do is read some pairs of values from a file, one on a line; for example, I might have a file that looks like this:
    Code:
    john
    7
    paul
    3
    luke
    15
    or like this:
    Code:
    ridley scott
    alien
    stanley cubrick
    odyssey 2001
    steven spielberg
    independence day
    Now I thought I can write one function to read them all, using templates:
    Code:
    template<int>
    int convert(char * buf)
    {
    	return atoi(buf);
    }
    
    template<string>
    string convert(char * buf)
    {
    	return string(buf);
    }
    
    template<class T1, class T2>
    vector<pair<T1, T2> > load_vector(ifstream & f)
    {
    	vector<pair<T1, T2> > v;
    	pair<T1, T2> p;
    
    	char buf[128];
    
    	while(!f.eof())
    	{
    		f.getline(buf, sizeof(buf));
    		p.first = convert<T1>(buf);
    		f.getline(buf, sizeof(buf));
    		p.second = convert<T2>(buf);
    
    		if(!p.first.empty())
    		{
    			v.push_back(p);
    		}
    	}
    
    	return v;
    }
    and I would call (say) load_vector<string, int> and get a nicely structured vector.
    Not.
    The compiler generates an error message, along the lines of
    'convert' : invalid template argument for 'function-parameter', compile-time evaluatable constant expression expected
    , which I don't really understand. Isn't <string, int> constant enough? MSDN didn't help, either.
    Thank you for your time

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    try something like load_vector<make_pair<string, int> >. not sure of the syntax.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You're not using templates correctly. Maybe something like this:
    Code:
    template<typename T>
    T convert(char * buf);
    
    template<>
    int convert<int>(char * buf)
    {
        return atoi(buf);
    }
    
    template<>
    string convert<string>(char * buf)
    {
        return string(buf);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  2. Trouble modifing my program from templates to link lists
    By PaulTodd03 in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2005, 02:15 PM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. Having trouble with templates
    By oasamostexianu in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2004, 02:42 PM