-1

I'm trying to specialize a methode based on the type of the first argument of the class template

template<class T, class TOut = double >
class Histogram  
{
    // ...
    void resetMinMaxVal();
}

template<class TOut>
inline void Histogram<int, TOut>::resetMinMaxVal()
{
    // ...
}

template<class TOut>
inline void Histogram<long, TOut>::resetMinMaxVal()
{
    // ...
}

I can't nail the syntax, seems to be this should be possible ?

Mario
  • 302
  • 1
  • 8
  • Syntax looks fine for me, what's the exact error you get? – πάντα ῥεῖ Jul 14 '14 at 15:54
  • The answer was given in another post ( I look for answer before posting but didn't find that one ). Seems like the language C++ does not support partial method specialization. – Mario Jul 15 '14 at 15:30
  • _'Seems like the language C++ does not support partial method specialization'_ Exactly. I didn't realize first, you're looking for partial specialization. – πάντα ῥεῖ Jul 15 '14 at 15:43

1 Answers1

3

In this case you are specializing the whole class, so you need to declare the specializations for the whole class.

Like this:

#include <iostream>

template<class T, class TOut = double>
class Histogram
{
public:
    void resetMinMaxVal() {
        std::cout << "Generic method\n";
    }
};

template<class TOut>
class Histogram<int, TOut>
{
public:
    void resetMinMaxVal() {
        std::cout << "int method\n";
    }
};

template<class TOut>
class Histogram<long, TOut>
{
public:
    void resetMinMaxVal() {
        std::cout << "long method\n";
    }
};

int main(int argc, char* argv[])
{
    Histogram<int> hint;
    hint.resetMinMaxVal(); //prints 'int method'

    Histogram<long> hlong;
    hlong.resetMinMaxVal(); //prints 'long method'

    Histogram<double> hdouble;
    hdouble.resetMinMaxVal(); //prints 'Generic method'

    return 0;
}
Gustavo Muenz
  • 9,278
  • 7
  • 40
  • 42
  • Thanks, unfortunately Histogram is quite big and resetMinMaxVal() is the only method that I want to specialize. – Mario Jul 15 '14 at 15:28