6中与模板函数相关的错误

6中与模板函数相关的错误

本文介绍了GCC 3.4.6中与模板函数相关的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在办公室遇到了一个奇怪的编译错误,我怀疑它是我们版本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 '>' token
test.cpp:26: error: expected primary-expression before ')' token

通过引入临时变量来存储第一条语句的结果( bar.value(yoyo)),可以避免错误。谁能告诉我是什么原因造成的?它是GCC 3.4.6中的错误(它似乎在GCC 4.xx中有效),并且在这个版本中是否还有其他类似的与模板相关的错误?

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;
}


推荐答案

p>

Try this instead:

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

就我所见,问题出在独立名称上,类似于您有时需要前缀类型与 typename

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

以上指定给编译器 doIt 是模板成员方法,而不是使用小于运算符进行比较的成员变量 doIt

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.

这篇关于GCC 3.4.6中与模板函数相关的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:58