I am trying to test below code but getting compile error:
TemplateTest.cpp:44:13: error:expected primary-expression before 'int'
h_.handle<int>(i);
build command:
g++ -c -std=c++11 -g -O2 -Wall -Werror TemplateTest.cpp -o TemplateTest.o
#include <iostream>
using namespace std;
enum PROTOCOL {
PROTO_A,
PROTO_B
};
// ----- HandlerClass -------
template <PROTOCOL protocol>
class Handler {
public:
template <class TMsg>
bool handle(const TMsg&) {
return false;
}
};
template <>
template <class TMsg>
bool Handler<PROTO_A>::handle(const TMsg&) {
cout << "PROTO_A handler" << endl;
return true;
}
template <>
template <class TMsg>
bool Handler<PROTO_B>::handle(const TMsg&) {
cout << "PROTO_B handler" << endl;
return true;
}
// ----- DataClass ------
template <PROTOCOL protocol>
struct Data {
typedef Handler<protocol> H; //select appropriate handler
H h_;
int i;
Data() : i() {}
void f() {
h_.handle<int>(i); //***** <- getting error here
}
};
int main () {
Data<PROTO_A> b;
b.f();
return 0;
}