when compiling with mingw32-g++, there is error: no matching function for call to ‘for_each(int [9], int*, main()::Output)’, but can do well in vs2005?
#include <iostream>
#include <algorithm>
int main() {
struct Output {
void operator () (int v) {
std::cout << v << std::endl;
}
};
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
std::for_each(a, a + sizeof(a)/sizeof(a[0]), Output());
return 0;
}
In pre-C++11 version of the language types used as template arguments were required to have linkage. Local class declarations in C++ have no linkage, which is why they can’t be used as template arguments in C++98/C++03. In those versions of the language you have to declare your class in namespace scope.
The linkage requirement was removed in C++11. Your code is valid from C++11 point of view. Apparently, you are compiling in pre-C++11 mode.