I have tried to make a SumVector function for all types, but if it is called with a vector of unsigned char's, it will do something special. Here is what I tried, but I get a multiple definitions of the function compiler error.
template <typename T>
void SumVector(vector<T> &V)
{
T Sum = 0;
for(unsigned int i = 0; i < V.size(); i++)
Sum += V[i];
cout << "Sum: " << Sum << endl;
}
template <>
void SumVector<unsigned int>(vector<unsigned int> &V)
{
unsigned int Sum = 0;
for(unsigned int i = 0; i < V.size(); i++)
Sum += V[i];
cout << "Unsigned int sum: " << Sum << endl;
}
Also, am I right that this is called "partial template specialization"? I saw a bunch of tutorials but I couldn't gather exactly what normal "template specialization" was, it looked they were just defining a separate function for each type, just like overloading?
Thanks,
Dave