C++具有约束力的问题
有没有办法让 boost::bind
与 std::fill
一起工作?
我尝试了以下方法,但没有成功:
boost::bind(std::fill, x.begin(), x.end(), 1);
Is there any way to make boost::bind
work with std::fill
?
I tried the following, but it didn't work:
boost::bind(std::fill, x.begin(), x.end(), 1);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于
std::fill
是一个模板函数。可以这么说,模板函数在被实例化之前并不真正存在。您无法获取std::fill
的地址,因为它实际上并不存在;它只是使用不同类型的类似函数的模板。如果您提供模板参数,它将引用模板的特定实例,一切都会好起来的。std::fill
函数有两个模板参数:ForwardIteratorType,它是容器迭代器的类型,以及 DataType,它是容器所容纳的类型。您需要提供两者,以便编译器知道您要使用 std::fill 模板的哪个实例化。The problem is that
std::fill
is a template function. Template functions don't really exist, so to say, until they're instantiated. You can't take the address ofstd::fill
because it doesn't really exist; it's just a template for similar functions that use different types. If you provide the template parameters, it will refer to a specific instantiation of the template, and everything will be okay.The
std::fill
function has two template parameters: ForwardIteratorType, which is the type of an iterator to the container, and DataType, which is the type that container holds. You need to provide both, so the compiler knows which instantiation of thestd::fill
template you want to use.