Parse Error in template class

D

Dave O'Hearn

This is a stripped down example that duplicates the problem I am
having. If I do it with concrete types, I can access the set's
iterator type correctly. If I make a template with generic types, I
can't get it right.

#include <set>

// concrete version. this compiles fine.
class it_works
{
private:
typedef std::pair<int, double> pair_t;
typedef std::set<pair_t> set_t;

set_t my_set;

public:
void func() {
set_t::iterator i;
std::set<pair_t>::iterator j;
std::set< std::pair<int, double> >::iterator k;
}
};

// generic version, templated on T1 and T2. it doesn't work.
template<typename T1, typename T2>
class doesnt_work
{
private:
typedef std::pair<T1, T2> pair_t;
typedef std::set<pair_t> set_t;

set_t my_set;

public:
void func() {
set_t::iterator i; // "parse error before `;' token"
std::set<pair_t>::iterator j; // same error
std::set< std::pair<T1, T2> >::iterator k; // same error
}
};

It seems to be just a stupid syntax error. If I replace one of the
lines with "BLAH something = _abcdefg_;", I get the same error. But I
can't figure out why I get the parsing errors on the generic version,
but not the concrete version. Testing a concrete version is usually
the process I go through to track down parse errors in a template, but
it didn't throw any light on things this time.

If it helps, I am using gcc-3.2.2, but I am fairly sure it is just a
syntax error.
 
D

Dave O'Hearn

Gianni Mariani said:
In a template it's sometimes impossible to tell if the token you are
about to parse is part of a type declaration or an identifier because it
is incomplete (by definition). To get around this, the keyword
"typename" is used to indicate to the compiler what it is.

Thanks. I had heard stories about the need to occasionally throw
"typename" around, but I figured it only happened in very weird
meta-template code and it would be years before I had to learn what it
meant. I guess not!
gcc 3.2.2 has some (many?) template parsing bugs. I used gcc 3.3 and
the messages were pretty good.

I am waiting for 3.4... just a few more months now!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top