bl**********@gmail.com wrote:
[snip]
template<typename T1,typename T2>
vector<T2> abso(const vector<T1>& Vin)
{
vector<T2> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);
}
[snip]
I dont like that syntax very much and have decided to use function
overload instead. What are the pros and cons of such approach??
vector<float> abso(const vector<CS>& Vin)
{
vector<float> Vout(Vin.size());
for(int i=0;i<Vin.size();i++)
Vout[i]=abs(Vin[i]);
}
[snip]
Well, if you want the function to work for various vector types, you'd
have to rewrite the same code for each overload.
Note that with the original code, if you reverse the template
arguments, you only have to specify one of the arguments, the one that
can't be deduced:
template<typename T2,typename T1>
vector<T2> abso(const vector<T1>& Vin) { ... }
int main() {
...
vector<int> ret = abso<int>(otherVec);
}
Or, you could not return a vector at all, and then all the arguments
would be deduced:
template<typename T1,typename T2>
void abso(const vector<T1>& Vin, vector<T2> &Vout) { ... }
int main() {
...
abso(in, out);
}
you also might want to consider std::transform instead or std::valarray.