OiO.lk Blog templates Template specialization for enums
templates

Template specialization for enums


I have a generic template class, that I do not want (unable) to change

template <typename TData>
class Printer {
 public:
    bool Write(std::ostream& ostr, const TData& data) const {
        ostr << data;
        return true;
    }
};

I want to implement a specialized class for Printer that works for enums only

template <typename TThatIsEnum>
class Printer{
 public:
    bool Write(std::ostream& ostr, const TThatIsEnum& data) const {
        static Printer<int> int_printer;

        return int_printer.Write(ostr, static_cast<int>(data));
    }
};

Is this possible without adding that additional template parameter to generic template?



You need to sign in to view this answers

Exit mobile version