#include <vector>
#include <iostream>
#include "boost/function.hpp"
template <class T1, class T2, class T3>
static void
FOREACH (T1 cont, boost::function<T2(T3)> callback) {
typename T1::iterator it = cont. begin ();
for ( ; it != cont. end(); it++ ) {
callback (*it);
}
}
static void
Print (int number)
{
std:: cout << number << std:: endl;
}
int main ()
{
std:: vector<int> vec;
for ( int i=1; i <= 10; ++i ) vec. push_back (2*i);
FOREACH ( vec, fun );
return 0;
}
为什么上面的代码不能编译?如果我创建像下面这样的专用版本,它可以正常工作。
static void
FOREACH (std:: vector<int> cont, boost::function<void(int)> callback) {
std:: vector<int>:: iterator it = cont. begin ();
for ( ; it != cont. end(); it++ ) {
callback (*it);
}
}
请有人告诉我如何将 boost::function 与函数模板一起使用?