gcc incorrectly handles the following code.  The underlying problem is that gcc
does not require the "template" keyword in certain places where it should
require it.  The side effect is that gcc refuses to parse valid code that looks
like a template parameter list but really is not.  The first templated function
call in the main() should be rejected by gcc because the template keyword
should be required.  The second templated function call is rejected but should
be accepted because it's parseable as a combination of "<" and ">" operators
and makes sense in that way (Visual Studio accepts this code, again
incorrectly; presumably it does not construct a single parse tree).
------------
merlin:~:  gcc tmp.cc
tmp.cc: In function 'int MyTemplatedFunction() [with A = char, int B = 6]':
tmp.cc:29:   instantiated from here
tmp.cc:23: error: 'MyClass<6>' is not a base of 'MyDerivedClass<char, 6>'
----------
#include <iostream>

template<int B> class MyClass {
 public:
  int MyFunc() {return B;}
};

template<class A, int B> class MyDerivedClass: public MyClass<B> {
 public:
  int MyFunc() {return 1;}
};

template<int B> class MyDerivedClass<char, B> {
 public:
  int MyClass;
  MyDerivedClass(): MyClass(10) {}
};

int MyFunc() { return 5; }

template<class A, int B>  int MyTemplatedFunction() {
  MyDerivedClass<A,B> *ptr = new MyDerivedClass<A,B>;
  return ptr->MyClass<B>::MyFunc(); 
  // should be ptr->template MyClass<B>::MyFunc()
// to be parsed as template, but gcc does not require this.
}

int main()
{
  std::cout << "First time: " <<MyTemplatedFunction<int,6>() << std::endl;
  std::cout << "Next time: " << MyTemplatedFunction<char,6>() << std::endl;
  while(1);
  return 0;
}


-- 
           Summary: template keyword incorrectness// failure to parse valid
                    code.
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dpovey at gmail dot com
 GCC build triplet: i686-linux-gnu
  GCC host triplet: i686-linux-gnu
GCC target triplet: i686-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45374

Reply via email to