+2  A: 

If the only thing that is different is the match function, then have the class call the match function using a function pointer instead of adding the match function inside of the class (similar to C's qsort function). Write your two match routines as independent functions, and assign each instance of your class a pointer to the appropriate match function. It's admittedly a C-ish approach to the problem but it should work.

bta
I wouldn't say it's so cish. c++ map, set, and others allow a compare function as an argument to do much the same thing. I think it's a pretty good approach.
JoshD
This is an interesting approach but the problem I have with this is that it requires that the coder specifically associate the match function with the template instance. Or am I missing something from your suggestion?
John Rocha
+1  A: 

If the only thing that behaves differently is a single function, then you don't have to specialize the whole class, you can just specialize that function. I'm not sure if there's syntax to do this when the function is defined within the body of the class, but if you define the function externally, then you can do it like this:

template <class T>
class X
{
    void f();
};

template <class T>
void X<T>::f()
{
    // general code
}

template<>
void X<std::string>::f()
{
    // specialized code
}

For multiple template parameters

template<int K, typename T> class X;
template<int K, typename T> void friend_func(X<K,T> &);

template<int K, typename T>
class X
{
public:
    void class_func();
    friend void friend_func<>(X &);
};

template<int K, typename T>
void X<K,T>::class_func()
{
    friend_func(*this);
}

template<int K, typename T>
void friend_func(X<K,T> & x)
{
    // non specialized version
}

template<int K>
void friend_func(X<K,std::string> & x)
{
    // specialized version
}
PigBen
I was able to get this solution to work with the problem I stated. But then I realized I over simplified my problem. I actually have two template parameters, and I cannot get this solution to work with two parameters. Can you please read my updates and provide additional insights?
John Rocha
Unfortunately, you cannot partially specialize template functions. However, there is an eloquent solution that uses overloaded friend functions. See my updated answer. Note that they don't have to be friend functions. But I did it that way so that the function can emulate a member function, treating the passed parameter as *this.
PigBen
+1  A: 

You can have a match() function template delegate to a class template member function. Then you can specialize the class template:

// primary template for general-purpose matching
template <typename T>
struct match_impl
{
    static bool match(const T& x) { return true; }
};

// specialization for std::string matching
template <>
struct match_impl<std::string>
{
    static bool match(const std::string& x) { return true; }
};

template <typename T>
bool match(const T& x)
{
    return match_impl<T>::match(x);
}
James McNellis