You can, but you need to specialize the entire class "myclass", and not just one method "myfunc". Here is an example:
#include <iostream> template <typename K, typename V> class myclass { public: void myfunc(K key, V value) { std::cout << "non-specialized " << key << "->" << value << std::endl; } }; template<typename V> class myclass<int, V> { public: void myfunc(int key, V value) { std::cout << "specialized " << key << "->" << value << std::endl; } }; int main(int argc, char* argv[]) { myclass<int, char> instance; instance.myfunc(10, 'a'); myclass<long, char> instance2; instance2.myfunc(10L, 'a'); return 0; }
source share