This is the continuation of this question.
I have different template methods, each one for a different kind of message, using non-type template parameters and template specialization:
namespace Handlers {
enum MSG_TYPES {
MSG1,
MSG2
};
template<MSG_TYPES>
void handle_message() {
// Default handler : not defined type
}
template<>
void handle_message<MSG1>() {
cout << "Handle 1";
}
template<>
void handle_message<MSG2>() {
cout << "Handle 2";
}
Now, I'd like to have some other method to dispatch to the correct handler. Something like
template<typename T>
void handle(T t) {
try {
handle_message<T>();
} catch(...) {
}
}
which could be invoked like
int i = 0;
Handlers::handle(static_cast<Handlers::MSG_TYPES>(i));
So, how this dispatcher could be accomplished ?
PS: The previous code fails on handle_message<T>();
because of
note: template argument deduction/substitution failed:
shouldn't the default handler be invoked ?