@sep
"A simple solution
The answer posted by "sep" is pretty good, probably good enough for 99% of application developers, but might use some improvement if it is part of the library interface to repeat:
Specializing for Vector:
template<typename C> void mySuperTempalte (std::vector<C> myCont) {
This will work if the caller does not use std :: vector. If this works well enough for you to specialize in vector, list, etc., then stop here and just use it.
More complete solution
First, note that you cannot partially specialize function templates — you can create overloads. And if two or more of them coincide with the same degree, you will get "ambiguous overload" errors. Therefore, we need to make exactly one match in each case that you want to support.
One of the methods for this is to use the enable_if method - enable_if allows you to selectively intercept function templates from a list of possible matches using an obscure language rule ... basically, if some logical expression is false, the overload becomes "invisible". See SFINAE for more information if you are interested.
Example. This code can be compiled from the command line using MinGW (g ++ parameterize.cpp) or VC9 (cl / EHsc parameterize.cpp) without errors:
#include <iostream> #include <vector> #include <string> using namespace std; template <bool B, class T> struct enable_if {}; template <class T> struct enable_if<true, T> { typedef T type; }; template <class T, class U> struct is_same { enum { value = false }; }; template <class T> struct is_same<T,T> { enum { value = true }; }; namespace detail{ // our special function, not for strings // use ... to make it the least-prefered overload template <class Container> void SpecialFunction_(const Container& c, ...){ cout << "invoked SpecialFunction() default\n"; } // our special function, first overload: template <class Container> // enable only if it is a container of mutable strings typename enable_if< is_same<typename Container::value_type, string>::value, void >::type SpecialFunction_(const Container& c, void*){ cout << "invoked SpecialFunction() for strings\n"; } } // wrapper function template <class Container> void SpecialFunction(const Container& c){ detail::SpecialFunction_(c, 0); } int main(){ vector<int> vi; cout << "calling with vector<int>\n"; SpecialFunction(vi); vector<string> vs; cout << "\ncalling with vector<string>\n"; SpecialFunction(vs); }
Output:
d:\scratch>parameterize.exe calling with vector<int> invoked SpecialFunction() default calling with vector<string> invoked SpecialFunction() for strings d:\scratch>