C++绑定问题

Yip*_*Yay 1 c++ bind fill

有没有什么办法,使boost::bind工作与std::fill

我尝试了以下,但它不起作用:

boost::bind(std::fill, x.begin(), x.end(), 1);
Run Code Online (Sandbox Code Playgroud)

Col*_*nee 10

问题是这std::fill是一个模板功能.模板函数实际上并不存在,所以说,直到它们被实例化.你不能拿地址,std::fill因为它确实不存在; 它只是使用不同类型的类似函数的模板.如果您提供模板参数,它将引用模板的特定实例,一切都会好的.

std::fill函数有两个模板参数:ForwardIteratorType,它是容器迭代器的类型,以及DataType,它是容器所拥有的类型.您需要同时提供这两个,因此编译器知道std::fill您要使用的模板的实例化.

std::vector<int> x(10);
boost::bind(std::fill<std::vector<int>::iterator, int>, x.begin(), x.end(), 1);
Run Code Online (Sandbox Code Playgroud)