Partial specialization

Now, we are getting to the really interesting part of the C++ template programming—partial template specializations. When a class template is partially specialized, it remains as generic code, but less generic than the original template. The simplest form of the partial template is one where some of the generic types are replaced by concrete types, but other types remain generic:

template <typename N, typename D>class Ratio {    .....};template <typename D>class Ratio<double, D> {    public:    Ratio() : value_() {}    Ratio(const double& num, const D& denom) : value_(num/double(denom)) {}    explicit operator double() const { return value_; }    private:    double value_;};

Here, we convert the Ratio to a double value if the numerator ...

Get Hands-On Design Patterns with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.