I have a code like:
class Client2ServerProtocol {
};
class ProtocolHelper {
public:
template<class ProtocolClass>
int GetProtocolId() {
return -1;
}
};
template<> inline int
ProtocolHelper::GetProtocolId<Client2ServerProtocol>() {
return 1;
}
template<typename PROTOCOL_HELPER>
class Dispatcher {
public:
template<typename PROTOCOL_CLASS>
void Subscribe(int msgId) {
int protoId = helper.GetProtocolId<PROTOCOL_CLASS>();
printf("Subscribe protoId %d, msgId %d", protoId, msgId);
}
PROTOCOL_HELPER helper;
};
int main() {
Dispatcher<ProtocolHelper> dispatcher;
dispatcher.Subscribe<Client2ServerProtocol>(1);
return 0;
}
It compiles successfully (and works) under MSVC, but gcc is complaining about invalid syntax:
test.cc:23:56: error: expected primary-expression before ‘>’ token int protoId = helper.GetProtocolId();
test.cc:23:58: error: expected primary-expression before ‘)’ token
What i'm doing wrong? int protoId = helper.GetProtocolId();
You just need to put the template
keyword to signify that it follows up a template:
int protoId = helper.template GetProtocolId<PROTOCOL_CLASS>();
^^^^^^^^