I have a function for error reporting that is templated because it can report errors for many different message classes:
template <typename MSG>
void reportErr(const MSG& msg)
{
std::cout << "ERROR: " << msg.error << std::endl;
}
However, some types of message have more detailed error that can be reported or other specialized error reporting, e.g.
template<>
void reportErr(const SpecificMsg& msg)
{
std::cout << "ERROR: " << msg.error;
std::cout << ", details: " << msg.details << std::endl;
}
Since there are many types like SpecificMsg
, I'd rather not create an individual template specialization for each type. Is it possible to create a generic specialization/partial specialization for any type that has a .details
member variable?
If possible, I'd like a way to do this generally (so one specialization if it has .details
, a different one if it has .other_info
, etc).
Edit: This is explicitly asking about functions. I've seen code that does similar things to specialize template classes, but I've never encountered something that does what I want for non-member functions. I suspect it isn't hard to convert the approach used for classes to work for functions, but I haven't been able to figure out how to do it.
Edit 2: my version of gcc (4.6.3) appears not to support the full C++11 standard, so the void_t
option mentioned in the "duplicate" question doesn't work for me. My compiler complains "expected nested-name-specifier before 'type'" etc and won't even let me define void_t. As such, I've removed the C++11 tag from my question.