Bugs related to template-functions in GCC 3.4.6
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 开发者_JS百科'>' 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;
}
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.
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.
精彩评论