tags:

views:

129

answers:

2

I ran into a strange compile error at the office today and I'm suspecting it to be a bug in our version of GCC (3.4.6). I've been able to boil it down to a few lines of code (below). The compile error I get is:

test.cpp:26: error: expected primary-expression before '>' token
test.cpp:26: error: expected primary-expression before ')' token

The error can be avoided by introducing a temporary variable to store the result of the first statement (bar.value("yoyo")). Can anyone tell me what causes this? Is it a bug in GCC 3.4.6 (it seems to work in GCC 4.x.x) and are there other similar template-related bugs in this version?

class Foo
{
public:
  template<typename T> bool doIt() const { return true; }
};

class Bar
{
public:
  Foo value(const char * key)
  {
    return Foo();
  }
};

template<typename T>
void
mytestfunc()
{
   Bar bar;
   // Works fine:
   Foo foo = bar.value("yoyo");
   foo.doIt<T>();

   // Does not work on gcc 3.4.6:
   bar.value("yoyo").doIt<T>();
}

int main(int argc, char * args[])
{
  return 0;
}
A: 

The compiler should be creating a temporary in the last statement, that works exactly like "foo" in the previous two statements. So yeah, I think it's a bug, especially since it works in GCC 4. On the other hand, that's some pretty unusual code, so it may not turn up too often in practice. Generally I've found 3.4 to be pretty solid template-wise, particularly compared to some of the proprietary compilers.

A. L. Flanagan
+3  A: 

Try this instead:

 bar.value("yoyo").template doIt<T>();

As far as I can see, the problem is with dependent names, similar to how you sometimes need to prefix types with typename.

The above specifies to the compiler that doIt is a template member method, and not a member variable doIt that is being compared using the 'less than' operator.

jalf
Thx for the clarification, that works. Not the prettiest syntax around though...
larsm
The truth is that the name is not dependent in this case and `template` keyword is not required. Looks like a bug in the compiler. Comeau, for one example, compiles it as is.
AndreyT