C ++绑定问题

时间:2010-08-03 21:58:51

标签: c++ bind fill

有没有办法让boost::bindstd::fill一起使用?

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

boost::bind(std::fill, x.begin(), x.end(), 1);

1 个答案:

答案 0 :(得分: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);
相关问题