Marco A.'s solution is good,I want to provide more details ,and show why Macro A's answer is better.
solution 1:
As you make the return type of operator+ as the 3rd parameter ,so when explicity declare template parameter ,we have to specify all three parameter,like this:
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
// declare return type as the 3rd parameter
template <class S, class T, class U> std::vector<U> operator+(const std::vector<S> &a, const std::vector<T> &b){
std::vector<U> result(a.size());
for(int i = 0; i < a.size();++i){
result[i] = a[i] + b[i];
}
return result;
}
int main(int argc, char** argv) {
std::vector<double> bla;
bla.push_back(3.14);
bla.push_back(2.7);
// explicity specify three template parameter
bla = ::operator+<double,double,double>(bla,bla);
// print results
std::ostream_iterator<double> os_it(std::cout," ");
std::copy(bla.begin(),bla.end(),os_it);
std::cout << std::endl;
return 0;
}
output is :
6.28 5.4
solution 2: you can decalre the return type as the first parameter,then we only have to specify the return type ,and the other two parameter can be deduce by compiler .
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
//decalre return type as first template parameter
template <class U, class S, class T> std::vector<U> operator+(const std::vector<S> &a, const std::vector<T> &b){
std::vector<U> result(a.size());
for(int i = 0; i < a.size();++i){
result[i] = a[i] + b[i];
}
return result;
}
int main(int argc, char** argv) {
std::vector<double> bla;
bla.push_back(3.14);
bla.push_back(2.7);
// only explicity specify the return type
bla = ::operator+<double>(bla,bla);
// print results
std::ostream_iterator<double> os_it(std::cout," ");
std::copy(bla.begin(),bla.end(),os_it);
std::cout << std::endl;
return 0;
}
output is :
6.28 5.4
solution 3 : As Macro A. gave ,that is better than the above two solution. we needn't have to explicity specify the return type.As a complete example ,I also put my runnable example code here :
//Note: This file should be compiled with c++11
#include <iostream> // std::cout
#include <iterator> // std::ostream_iterator
#include <vector> // std::vector
#include <algorithm> // std::copy
#include <type_traits> // std::common_type c++11
//using std::common_type
template <class S, class T> std::vector<typename std::common_type<S, T>::type> operator+(const std::vector<S> &a, const std::vector<T> &b){
std::vector<typename std::common_type<S, T>::type> result(a.size());
for(int i = 0; i < a.size();++i){
result[i] = a[i] + b[i];
}
return result;
}
// print results
template <typename T>
void printData(const std::vector<T>& bla) {
std::ostream_iterator<double> os_it(std::cout," ");
std::copy(bla.begin(),bla.end(),os_it);
std::cout << std::endl;
}
int main(int argc, char** argv) {
std::vector<double> dvec;
dvec.push_back(3.14);
dvec.push_back(2.7);
std::vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
// we need not to explicity specify template parameter
printData(dvec+dvec);
printData(dvec+ivec);
printData(ivec+ivec);
return 0;
}
output is :
6.28 5.4
4.14 4.7
2 4
std::common_type: en.cppreference.com/w/cpp/types/common_type - ascheplerbla+bla, but there's not enough information there to deduce the template parameterU. - Mooing Duckstd::transform- Neil Kirkstd::valarrayalready implements this. - chris