given the following code

bool space(char c)
{
	return isspace(c);
}
bool not_space(char c)
{
	return !space(c);
}

template <class Out>

void split(const string& str, Out os){

	typedef string::const_iterator iter;

	iter i = str.begin();
	while(i != str.end()){
		i = find_if(i, str.end(), not_space);

		iter j = find_if(i, str.end(), space);

		if(i != str.end())
			*os++ = string(i, j);

		i = j;
	}
}

when I put this into a separated source file and call "split" I get an error but when it's in the same source file as main I don't, any ideas why ?

Is the file with main including your template's source file?

Yeah, it looks like it should work (to me), I get an "unresolved token" error

How should the header file for this function be? anything special since it contains a template ?

Edited 5 Years Ago by mitrious: n/a

You need to think about how templates work. When you write vector<int> compiler have to generate code specially for vector of ints, vector<int> and vector<char> are two different classes. If you use templates it is necessary to have definition of them while compiling objects files for every .cpp file which uses them. How would compiler know what to put in template object file?
Common practise is to put whole implementation of template functions and classes in a h files, it is identical to putting them in the same cpp file you are using them. This leads to one of main drawbacks of using templates, which is slow compilation (others are e.g. bad error messages).

Comments
Really useful and well explained

Yeah, it looks like it should work (to me), I get an "unresolved token" error

How should the header file for this function be? anything special since it contains a template ?

The "unresolved token" error usually means that you have used something declared in your code, but not implemented. If it gives a line number, carefully check that line of code for anything you declared in the program, and check that to see it has a proper definition.

Edited 5 Years Ago by fishsicles: n/a

You need to think about how templates work. When you write vector<int> compiler have to generate code specially for vector of ints, vector<int> and vector<char> are two different classes. If you use templates it is necessary to have definition of them while compiling objects files for every .cpp file which uses them. How would compiler know what to put in template object file?
Common practise is to put whole implementation of template functions and classes in a h files, it is identical to putting them in the same cpp file you are using them. This leads to one of main drawbacks of using templates, which is slow compilation (others are e.g. bad error messages).

Thx, I get what you mean,it was really useful

May I suggest that you read the following explanation.

One solution to your problem is explicit instantiation of the template. Here is an example of a function template. This is, of course, only applicable if you have a reasonably small amount of predetermined types for which your template could be instantiated. In real-life, this technique will make the compilation of the template code take a very long time, but it will make all the code that use that template code very fast to compile.

Comments
Offered a pretty complete solution and really useful info

May I suggest that you read the following explanation.

One solution to your problem is explicit instantiation of the template. Here is an example of a function template. This is, of course, only applicable if you have a reasonably small amount of predetermined types for which your template could be instantiated. In real-life, this technique will make the compilation of the template code take a very long time, but it will make all the code that use that template code very fast to compile.

Aw thx, I'm reading everything there, pretty useful for beginners, thx

This article has been dead for over six months. Start a new discussion instead.