13

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();

ejkoy
  • 197
  • 1
  • 1
  • 8

1 Answers1

26

You just need to put the template keyword to signify that it follows up a template:

int protoId = helper.template GetProtocolId<PROTOCOL_CLASS>();
                     ^^^^^^^^
101010
  • 41,839
  • 11
  • 94
  • 168
  • Thank you. This saved my day. But, where did this come from? Could you point to any more info about .template? – Armut Dec 04 '21 at 05:52
  • 2
    @Armut [The template disambiguator for dependent names](https://en.cppreference.com/w/cpp/language/dependent_name) – 101010 Dec 04 '21 at 22:09