How should I emplement templated functions in projects?
I read that if a function is templated, it can't be represented by a prototype, so the emplementation has to be in a header, or wherever you want to use it.
So, what if I have a large project? Do I just have to copy/paste the emplementation into every .cpp file I need it in, or is there a nice and clean way to use header files for this?
Here's an example of a templated funstion I most likely may use in a lot of .cpp files:
1 2 3 4 5 6 7 8 9 10 11
|
/**Converts any class type to another. Like from a string to an int,
or a float to a string, etc.. etc... */
template<class type1, class type2>
type2 conv(const type1& t1)
{
stringstream ss;
type2 t2 = type2();
ss<< type1;
ss>> type2;
return type2;
}
|
I often use this kind of conversion in menus with extra options that, for practical use, must be represented by characters instead of numbers, where numbers are also used. Thus, conversion is necessary.
If I'm wrong, and it is possible to prototype this, please post the prototype syntax for me, thanks for the help!
EDIT1:
I made the type1 arg a const