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

Check out post number 11 (by StuXYZ) in this thread (from earlier today).

hmm what I had found before was I had to leave all the implementations in the .h file or it wouldn't work... but here he has put them in the cpp file with only a declaration in the .h file... I'll try it out.

Do you know what I'm talking about with this "partial specialization" terminology?

Hi i am the original poster from the other thread referenced to you.

It's very difficult to get decent information,
I have actually been reading a oldstyle book,
to understand this.

If you just want it to work do as stuxyz told me.

On a deeper level,
I think partial specialization is used only for member functions of a templated class.
whereas templated functions are over loaded.


that is
a partial template will extend an already existing template
a function overload will create a completely new.

good luck

Ok so it seems you have to put the "generic" template function in the h file:

class.h
--------
template <typename T>
void Output(vector<T> &V)
{
	cout << endl << "other" << endl << "-------" << endl;
	for(unsigned int i = 0; i < V.size(); i++)
		cout << V[i] << endl;
		
}

and the specialization in the cpp file

class.cpp
--------
template <>
void Output<double>(vector<double> &V)
{
	cout << endl << "double" << endl << "----------" << endl;
	for(unsigned int i = 0; i < V.size(); i++)
		cout << V[i] << endl;
		
}

template <>
void Output<unsigned int>(vector<unsigned int> &V)
{
	cout << endl << "unsigned int" << endl << "----------" << endl;
	for(unsigned int i = 0; i < V.size(); i++)
		cout << V[i] << endl;
		
}

That look reasonable to everyone? It seems to work.

Thanks,

Dave

This seems to follow your solution but gives me a "multiple definition of void Output<unsigned int>(...)".

// main.h
#include "output.h"

int main () {
    std::vector <unsigned int> v;
    v.push_back (3);
    Output (v);
    return 0;
}
// output.h
#include <vector>
#include <iostream>

template <typename T>
void Output(std::vector<T> &V) {
	std::cout << "\nother\n-------\n";
	for (int i = 0; i < V.size(); i++)
		std::cout << V[i] << std::endl;
}
// output.cpp
#include "output.h"

template <>
void Output<double>(std::vector<double> &V) {
	std::cout << "\ndouble\n--------\n";
	for (int i = 0; i < V.size(); i++)
		std::cout << V[i] << std::endl;
}

template <>
void Output<unsigned int>(std::vector<unsigned int> &V) {
	std::cout << "\nunsigned int\n------------\n";
	for (int i = 0; i < V.size(); i++)
		std::cout << V[i] << std::endl;
}
commented: Template issue/well spotted. +5

Nucleon is 100% correct that it gives "multiple definition of void Output<unsigned int>" on standard compliant compilers.

The standard says that if you put implimentations in a declaration then ALL used specializations must be availiable at that instance.

Some compilers do help you out here BUT it is not standard and you should NEVER program to a single compiler unless ABSOLUTELY FORCED.

p.s Many thanks to nucleon for reading my previous post, I am surprised anyone except the original poster read that far!

StuXYZ: I didn't just READ your post, I studied it until I understood it because I found the situation a little confusing. It's actually pretty straightforward once you realize what the template class Foo<float>; in foo.cpp (from other thread) is doing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.