gcc bug with member template functions?

Steven Brown swbrown@ucsd.edu
Sat Mar 9 05:07:00 GMT 2002


When I call a templated member function with explicit template arguments from 
within a class which itself is templated, gcc (Cygwin's 2.95ish, RedHat 7.1's 
2.6ish, and latest 3.0.4) gives me a parse error.  Am I doing something 
syntactically wrong or is this a genuine gcc bug?


I've attached a testcase below.  class X has a templated 'get' function that 
you can call like x.get<int>().  This works in the main function and in a 
normal class, but used inside of a templated class it gives a parse error 
("gccbug.cc:14: parse error before `;' token" on newer gcc's, "gccbug.cc:14: 
parse error before `>'" on older).  My current workaround/hack is to define 
get to take a dummy argument (T const * = 0) so I can implicitly pass in the 
template argument like "x.get((int *) 0)" when needed.  See the comment about 
this in the code:

class X {

  public:
    template<typename T> T get(T const * = 0) {
      return T();
    }
};

template<typename T> class Yt {
  public:
    void test() {
      X x;
      // Try the other version here to see what I mean.
      x.get<int>();
      //x.get((int *) 0);
    }
};

class Y {
  public:
    void test() {
      X x;
      x.get<int>();
    }
};

int main() {
  X x;
  Y y;
  Yt<int> yt;

  x.get<int>();
}



More information about the Gcc-help mailing list