1
我不熟悉函数指针,我正在做一些测试。但在下面的程序中,我不明白为什么第一个版本能够工作,为什么第二个版本不能编译。什么是正确的语法?函数指针和std :: function:第一个编译而不是第二个?
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
template<typename Type>
void display(const std::vector<Type>& v)
{
if (!v.empty()) {
for (unsigned int i = 0; i < v.size()-1; ++i)
std::cout<<v[i]<<" ";
std::cout<<v[v.size()-1];
}
}
// Compiles
template<typename Type>
void apply1(std::vector<Type>& v, void(f)(Type*, Type*, Type))
{
f(&*v.begin(), &*v.end(), 0);
}
// Does not compile
template<typename Type>
void apply2(std::vector<Type>& v, std::function<void(Type*, Type*, Type)> f)
{
f(&*v.begin(), &*v.end(), 0);
}
int main()
{
std::vector<double> v = {1., 2., 3., 4., 5., 6.};
display(v); std::cout<<std::endl;
apply1(v, std::iota);
display(v); std::cout<<std::endl;
apply2(v, std::iota);
display(v); std::cout<<std::endl;
return 0;
}
误差如下:
error: cannot resolve overloaded function 'iota' based on conversion to type 'std::function<void(double*, double*, double)>'
所以,我怎么可以修改'apply2 '或者其调用像'std :: iota'这样的算法? – Vincent
@Vincent:编辑。 – Xeo
谢谢。但我有另一个问题:在我调用函数而不是函数模板的情况下:两个实现“等价”(除了std :: function更“强大”的事实外)? – Vincent