lately I've been toying around with templates and stumbled upon the following problem. I am implementing the CRTP pattern like this:
template<typename derived_t>
struct protocol_object
{
...
};
struct data_object : public protocol_object<data_object>
{
...
};
I now would like to match instances of class protocol_object
in a member template function, while still accepting non CRTP-types:
struct consumer_impl
{
template<typename derived_t>
void match(protocol_object<derived_t> &value)
{
std::cout << "protocol_class";
};
template<typename T>
void match(T &value)
{
std::cout << "any other type";
};
}
Unfortunately only the second version is ever called. Apparently match(protocol_object<derived_t> &value)
is not considered or rejected in favour of the more general form match(T &value)
.
data_object object;
double value;
consumer_impl consumer;
consumer.match(value); // yields "any other type" OK
consumer.match(object); // also yields "any other type" but want "protocol_class"
Is there a way out of this?
Thanks for any hints. Arne