Template function as for_each argument
Code:
/*
* Practice.cpp
*
* Created on: Sep 25, 2011
* Author: mike
*/
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
template <class T>
void printElement(const T& element) {
cout << element << ' ';
}
class OutputFunctor {
public:
template <class T>
void operator()(const T& val) { printElement(val); }
};
int main() {
vector<int> nums1;
for (int i = 1; i <= 10; ++i)
nums1.push_back(i);
// Compiler throws a fit
// for_each (nums1.begin(), nums1.end(), printElement);
// cout << '\n';
// Not a problem
for_each (nums1.begin(), nums1.end(), OutputFunctor());
cout << '\n';
return 0;
}
How come the compiler chokes on a template function, but greedily gobbles up my function object containing the same function?