vendredi 11 mai 2018

Specialize function if argument has member variable [duplicate]

This question already has an answer here:

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).

Aucun commentaire:

Enregistrer un commentaire