I have this code, if I change the function plus2 to a non-templated version, then, the code compiles and runs fine. But as it is, templated definition is not compiling. I get this error from MSVC .Net 2003:
c:\my\src\cpp\test-bed\test-bed.cpp(362) : error C2896: 'void forAll(T &,F)' : cannot use function template 'void plus2(T &)' as a function argument
c:\my\src\cpp\test-bed\test-bed.cpp(353) : see declaration of 'plus2'
c:\my\src\cpp\test-bed\test-bed.cpp(362) : error C2784: 'void forAll(T &,F)' : could not deduce template argument for 'T1 &' from 'std::vector<_Ty>'Code:#include <iostream> #include <vector> template <typename T, typename F> void forAll(T& array, F f) { for (int i=0; i<array.size(); i++) { f(array[i]); } } template <typename T> void plus2(T& v) { v += 2; } int main() { std::vector<int> vec; vec.push_back(10); vec.push_back(20); vec.push_back(30); forAll(vec, plus2); std::cout << vec[0] << " " << vec[1] << " " << vec[2] << std::endl; return 0; }