Skip to main content
GameDev.net gamedev.net
🔒 Locked

gcc templates problem

Started by Taldor Jan 14, 2007 at 3:11 AM 3 replies 800+ views
Original Post
Taldor
Taldor
When I try to compile the following code with gcc-4.1, I get an error: expected primary-expression before '>' token. Visual C++ 2005 does accept the code. Can someone explain why gcc complains, and how to fix it?
#include <iostream>
using namespace std;

template <typename T> class Base {
  public :
    template <typename W> void f () {
      cout << "f ()" << endl;
    }
};

template <typename T> class Derived: Base <T> {
  public :
    template <typename W> void g () {
      Base <T> :: f <W> (); // problematic line
    }
};

int main () {
  (new Derived <float> ()) -> g <int> ();
  return 0;
}
deffer
deffer
Quote:
Original post by Taldor
template class Derived: Base {
public :
template void g () {
Base :: f (); // problematic line
}
};


Unfortunately (for VC++, that is) gcc does a good job complaining there.
f is a dependent name, and just as dependent types should be precedded by typename keyword, dependent templates should be preceeded by template keyword.

template Base :: f ();


Visual Studio has a non-standard-conforming parser for C++, it ignores the typename and template keywords (in this context), either they are needed or not. It is a Bad Thing.
deffer
deffer
Lol, right [smile]
Taldor
Taldor
Thanks for the quick responses!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.